Examples

Live examples

Run Reactant modules in your browser and see how changes reach the view.

01 · State and actions

Counter

Run an action and watch the view read the new state with useConnector.

  • @state
  • @action
  • useConnector
demos/src/counter.tsxLive in your browser
counter.ts
@injectable({ name: 'counter' })
class Counter {
  @state count = 0;

  @action
  add(step: number) {
    this.count += step;
  }
}
Live previewReactant + React 18

Loading the live example…

02 · Dependency injection

Computed cart

Change the quantity and watch the total derive from state and an injected pricing service.

  • @computed
  • constructor injection
demos/src/cart.tsxLive in your browser
cart.ts
@injectable({ name: 'cart' })
class Cart {
  constructor(public pricing: Pricing) {}

  @state quantity = 1;

  @computed
  get total() {
    return this.quantity * this.pricing.unitPrice;
  }
}
Live previewReactant + React 18

Loading the live example…

03 · Reactant Share

Cross-tab counter

Open a second tab in the same browser and increase the count in either tab. Reactant Share synchronizes the state.

  • createSharedApp
  • SharedTab
  • delegate
demos/src/shared.tsxLive in your browser
shared.ts
const app = await createSharedApp({
  main: SharedCounterView,
  render: (view) => view,
  share: {
    name: 'reactant-docs-shared-counter',
    type: 'SharedTab',
    forcedSyncClient: true,
  },
});

await delegate(counter, 'increase', []);
Live previewReactant + React 18

Loading the live example…