A hook used to access a store created with createStore and bind it to a component.
createStore
Basic usage:
import { useStore } from "@fransek/statekit";import { store } from "./store";function Counter() { const { state: { count }, actions: { increment, decrement, reset }, } = useStore(store); return ( <div> <div>{count}</div> <button onClick={decrement}>-</button> <button onClick={increment}>+</button> <button onClick={reset}>Reset</button> </div> );} Copy
import { useStore } from "@fransek/statekit";import { store } from "./store";function Counter() { const { state: { count }, actions: { increment, decrement, reset }, } = useStore(store); return ( <div> <div>{count}</div> <button onClick={decrement}>-</button> <button onClick={increment}>+</button> <button onClick={reset}>Reset</button> </div> );}
With a select function:
const { state: { count },} = useStore(globalStore, (state) => state.counter); Copy
const { state: { count },} = useStore(globalStore, (state) => state.counter);
If the select function is provided, an equality check is performed. This has some caveats:
select
state.count
{ count: state.count }
The store created with createStore.
Optional
A function to select a subset of the state. Can prevent unnecessary re-renders.
An object containing the current state, actions, and set function.
A hook used to access a store created with
createStore
and bind it to a component.Example
Basic usage:
Example
With a select function:
Remarks
If the
select
function is provided, an equality check is performed. This has some caveats:state.count
){ count: state.count }
)