0.137.0 • Published 3 months ago

reactant-ssr v0.137.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

reactant-ssr

Node CI

SSR (Server-Side Rendering) can greatly improve the first rendering time, the most popular React SSR framework is Next.js. Currently, Reactant has supported SSR based on Next.js.

Installation

  • Build a Next.js application project using the create-next-app cli.
npx create-next-app reactant-ts-ssr --ts
cd reactant-ts-ssr
  • Install reactant-ssr.
yarn add reactant-ssr
  • Install other Babel development dependencies.
yarn add -D @babel/core @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators babel-plugin-parameter-decorator babel-plugin-transform-typescript-metadata

Configuration

  • Next.js with Typescript should use babel, and make sure you set:

Add .babelrc file

{
  "presets": ["next/babel"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": false }],
    "babel-plugin-parameter-decorator",
    "babel-plugin-transform-typescript-metadata"
  ]
}

Add this config to tsconfig.json file

+    "experimentalDecorators": true,
+    "emitDecoratorMetadata": true

Usage

  1. Define a module file for SSR src/index.tsx:
import React from 'react';
import {
  ViewModule,
  injectable,
  useConnector,
  action,
  state,
} from 'reactant-ssr';

@injectable()
export class Counter {
  @state
  count = 0;

  @action
  increase() {
    this.count += 1;
  }

  @action
  decrease() {
    this.count -= 1;
  }
}

@injectable()
export class CounterView extends ViewModule {
  constructor(public counter: Counter) {
    super();
  }

  component() {
    const count = useConnector(() => this.counter.count);
    return (
      <>
        <button type="button" onClick={() => this.counter.decrease()}>
          -
        </button>
        <div>{count}</div>
        <button type="button" onClick={() => this.counter.increase()}>
          +
        </button>
      </>
    );
  }
}
  1. Add custom Next.js app in pages/_app.tsx file.
import { createServerApp } from "reactant-ssr";
import { CounterView } from "../src/index";

export const app = createServerApp({
  modules: [CounterView],
});

export default app.bootstrap;
  1. Add home page routing page in pages/index.tsx file.
import { CounterView } from "../src/index";
import { app } from "./_app";

export default app.container.get(CounterView).component;
  1. run yarn dev and visit http://localhost:3000.

Q&A

  1. Does reactant-router support SSR?

reactant-router is based on React Router, which does not support SSR, so when you use reactant-ssr, your routing must usenext/router

  1. Does reactant-storage support SSR?

Yes, it works perfectly fine. In general, reactant-share is a different application model, so it also does not support SSR, which is supported by all other Reactant libraries.

Example

Documentation

You can visit reactant.js.org for more documentation.

0.137.0

3 months ago

0.133.0

3 months ago

0.125.0

5 months ago

0.129.0

4 months ago

0.136.0

3 months ago

0.132.0

3 months ago

0.128.0

4 months ago

0.124.0

6 months ago

0.131.0

4 months ago

0.135.0

3 months ago

0.127.0

4 months ago

0.123.0

7 months ago

0.130.0

4 months ago

0.134.0

3 months ago

0.126.0

4 months ago

0.121.0

9 months ago

0.120.0

9 months ago

0.119.0

10 months ago

0.122.0

9 months ago

0.118.0

10 months ago

0.114.0

11 months ago

0.110.0

1 year ago

0.117.0

10 months ago

0.113.0

11 months ago

0.116.0

10 months ago

0.112.0

11 months ago

0.115.0

11 months ago

0.111.0

12 months ago

0.109.0

1 year ago

0.108.0

1 year ago

0.107.0

1 year ago

0.106.0

1 year ago

0.105.0

1 year ago

0.104.0

1 year ago

0.103.0

1 year ago

0.102.0

1 year ago

0.101.0

1 year ago

0.99.0

2 years ago

0.100.0

2 years ago

0.98.0

2 years ago

0.87.0

2 years ago

0.95.0

2 years ago

0.97.0

2 years ago

0.91.0

2 years ago

0.93.0

2 years ago

0.88.0

2 years ago

0.90.0

2 years ago

0.96.0

2 years ago

0.94.0

2 years ago

0.89.0

2 years ago

0.86.0

2 years ago

0.85.0

2 years ago

0.81.0

2 years ago

0.83.0

2 years ago

0.84.0

2 years ago

0.80.0

2 years ago

0.82.0

2 years ago

0.79.0

2 years ago

0.78.0

2 years ago

0.74.0

2 years ago

0.76.0

2 years ago

0.72.0

2 years ago

0.77.0

2 years ago

0.73.0

2 years ago

0.75.0

2 years ago

0.62.0

3 years ago

0.64.0

3 years ago

0.60.0

3 years ago

0.59.0

3 years ago

0.57.0

3 years ago

0.70.0

2 years ago

0.67.0

3 years ago

0.69.0

2 years ago

0.63.0

3 years ago

0.65.0

3 years ago

0.61.0

3 years ago

0.58.0

3 years ago

0.71.0

2 years ago

0.66.0

3 years ago

0.68.0

3 years ago

0.55.0

3 years ago

0.53.0

3 years ago

0.51.0

3 years ago

0.56.0

3 years ago

0.54.0

3 years ago

0.52.0

3 years ago

0.42.0

3 years ago

0.21.0

3 years ago

0.43.0

3 years ago

0.20.0

3 years ago

0.40.0

3 years ago

0.41.0

3 years ago

0.19.0

3 years ago

0.39.0

3 years ago

0.38.0

3 years ago

0.37.0

3 years ago

0.36.0

3 years ago

0.35.0

3 years ago

0.34.0

3 years ago

0.33.0

3 years ago

0.17.0

3 years ago

0.18.0

3 years ago

0.32.0

3 years ago

0.31.0

3 years ago

0.30.0

3 years ago

0.50.0

3 years ago

0.29.0

3 years ago

0.28.0

3 years ago

0.48.0

3 years ago

0.27.0

3 years ago

0.49.0

3 years ago

0.26.0

3 years ago

0.46.0

3 years ago

0.25.0

3 years ago

0.47.0

3 years ago

0.24.0

3 years ago

0.44.0

3 years ago

0.23.0

3 years ago

0.45.0

3 years ago

0.22.0

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.0

3 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago