createStore
import { createStore } from '@zedux/react'
Creates a Zedux store. Prefer using injectStore()
to create stores inside atoms.
Stores can be either zero-config or configured with a reducer hierarchy.
Examples
import { createStore } from '@zedux/react'
const zeroConfigStore = createStore()
zeroConfigStore.setState('initial state')
const zeroConfigStoreWithInitialState = createStore(null, 'initial state')
zeroConfigStoreWithInitialState.setState('new state')
const reducerStore = createStore(myRootReducer)
reducerStore.dispatch({ type: 'my-action-type' })
const machineStore = createStore(stateMachine)
machineStore.dispatch({ type: 'my-action-type' })
const composedStore = createStore({
zeroConfig: zeroConfigStore,
reducerStore: reducerStore,
machineStore: machineStore,
})
const bigComposedStore = createStore({
a: storeA,
b: reducerB,
c: {
d: reducerD,
e: storeE,
f: {
g: storeG,
},
},
})
Signature
- Simplified
- TypeScript
createStore = (initialHierarchy?, initialState?) => store
declare const createStore: {
<State = any>(
initialHierarchy?: HierarchyDescriptor<State>,
initialState?: State
): Store<State>
<State = any>(
initialHierarchy: null | undefined,
initialState: State
): Store<State>
}
Optional. A HierarchyDescriptor detailing the shape of the store.
This is not passed for zero-config stores. For configured stores (stores with a reducer hierarchy), you don't have to pass this initially. You can configure the store after creation via store.use().
If an object is passed, createStore()
will spider through it and create a reducer hierarchy. createStore()
will then call the generated root reducer to get the initial state of the store.
For TS users, it is most convenient to pass this up-front with configured stores for automatic typing.
Optional. The initial state of the store.
This can be passed for configured or zero-config stores. You don't have to pass this initially. You can set the state after creation via store.setState().
For TS users, it is most convenient to pass this for automatic typing.
A Zedux Store.