Existing Store
Bind an existing @ilokesto/store instance to the React hook layer.
Existing Store
@ilokesto/state does not require you to start from plain initial state. If you already have a vanilla Store instance, you can attach the React hook layer to that existing store instead of rebuilding it.
The basic shape
The create function accepts either an initial state or an existing Store instance.
import { Store } from '@ilokesto/store';
import { create } from '@ilokesto/state';
// 1. Create a vanilla store
const myStore = new Store({ count: 0 });
// 2. Bind it to the React hook layer
const useCounter = create(myStore);When this is the right choice
- You already own the store elsewhere: maybe another runtime layer created it first.
- You want one source of truth across React and non-React code.
- You want to configure middleware before React enters the picture.
- You want
@ilokesto/stateto stay a thin binding, not the place where the store is born.
What does not change
Passing an existing Store does not create a second copy. create(store) uses that instance as-is.
- Existing middleware still runs.
- Existing state is preserved.
getInitialState()still matters for SSR snapshots.
The tradeoff is that lifecycle and ownership now live with the original store creator, not with the React layer.