0.109.0 • Published 8 days ago

reactant-ssr v0.109.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days 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.109.0

8 days ago

0.108.0

8 days ago

0.107.0

8 days ago

0.106.0

2 months ago

0.105.0

2 months ago

0.104.0

2 months ago

0.103.0

4 months ago

0.102.0

4 months ago

0.101.0

5 months ago

0.99.0

5 months ago

0.100.0

5 months ago

0.98.0

5 months ago

0.87.0

7 months ago

0.95.0

6 months ago

0.97.0

5 months ago

0.91.0

6 months ago

0.93.0

6 months ago

0.88.0

7 months ago

0.90.0

6 months ago

0.96.0

5 months ago

0.94.0

6 months ago

0.89.0

7 months ago

0.86.0

7 months ago

0.85.0

8 months ago

0.81.0

10 months ago

0.83.0

10 months ago

0.84.0

8 months ago

0.80.0

10 months ago

0.82.0

10 months ago

0.79.0

10 months ago

0.78.0

1 year ago

0.74.0

1 year ago

0.76.0

1 year ago

0.72.0

1 year ago

0.77.0

1 year ago

0.73.0

1 year ago

0.75.0

1 year ago

0.62.0

2 years ago

0.64.0

2 years ago

0.60.0

2 years ago

0.59.0

2 years ago

0.57.0

2 years ago

0.70.0

1 year ago

0.67.0

1 year ago

0.69.0

1 year ago

0.63.0

2 years ago

0.65.0

2 years ago

0.61.0

2 years ago

0.58.0

2 years ago

0.71.0

1 year ago

0.66.0

2 years ago

0.68.0

1 year ago

0.55.0

2 years ago

0.53.0

2 years ago

0.51.0

2 years ago

0.56.0

2 years ago

0.54.0

2 years ago

0.52.0

2 years ago

0.42.0

2 years ago

0.21.0

2 years ago

0.43.0

2 years ago

0.20.0

2 years ago

0.40.0

2 years ago

0.41.0

2 years ago

0.19.0

2 years ago

0.39.0

2 years ago

0.38.0

2 years ago

0.37.0

2 years ago

0.36.0

2 years ago

0.35.0

2 years ago

0.34.0

2 years ago

0.33.0

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.32.0

2 years ago

0.31.0

2 years ago

0.30.0

2 years ago

0.50.0

2 years ago

0.29.0

2 years ago

0.28.0

2 years ago

0.48.0

2 years ago

0.27.0

2 years ago

0.49.0

2 years ago

0.26.0

2 years ago

0.46.0

2 years ago

0.25.0

2 years ago

0.47.0

2 years ago

0.24.0

2 years ago

0.44.0

2 years ago

0.23.0

2 years ago

0.45.0

2 years ago

0.22.0

2 years ago

0.16.0

2 years ago

0.15.0

2 years ago

0.14.0

2 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.11.0

3 years ago