esp-js-react
The esp-js-react package connects React components to the stores on an esp-js EventBus.
Its peer dependencies are esp-js, esp-js-ui, and React 19. It uses esp-js-ui for its types only. See installation.
With esp-js-ui
If you build your app with esp-js-ui, each module binds its views to regions with withView.
EspAppwraps your React tree and gives it the app’s bus and modules.RegionViewrenders the views bound to one region.
On its own
Without esp-js-ui, you create the bus and register the stores yourself.
EspEventBusContextProvidergives the bus to the components below it.ConnectableComponentrenders a view with the model of one store, and renders it again each time the model changes.
import { createRoot } from 'react-dom/client';
import { EventBus } from 'esp-js';
import { ConnectableComponent, EspEventBusContextProvider, usePublishStoreEvent } from 'esp-js-react';
type CounterModel = { count: number };
const bus = new EventBus();
bus.storeBuilder<CounterModel>('counter', { count: 0 })
.withEventHandler('Increment', (draft) => {
draft.count++;
})
.build();
const CounterView = ({ model }: { model: CounterModel }) => {
const publishStoreEvent = usePublishStoreEvent();
return (
<button onClick={() => publishStoreEvent('Increment', {})}>
Count: {model.count}
</button>
);
};
createRoot(document.getElementById('root')!).render(
<EspEventBusContextProvider bus={bus}>
<ConnectableComponent storeId="counter" view={CounterView} />
</EspEventBusContextProvider>
);
ConnectableComponentpasses the store’s model toCounterViewas themodelprop.- A click publishes
Incrementto thecounterstore. The view renders again with the new count.
Pages in this section
- EspApp: render an esp-js-ui app, and get the bus from React.
- RegionView: render the views bound to a region.
- Rendering a view: connect a view to a store, and the hooks that read and publish.