š React custom hook for persist state management
The new method will retrieve the latest store value when an action is performed, this is great for usage such as re-render is not required and yet you want to retrieve the newest store value.
const update = (state: GlobalState, payload: string) => {
return {
...state,
value: payload
}
}
const { getState, actions } = useStateMachine({
update,
});
<button onClick={() => actions.update('test', { skipRender: true })}>Update<buttonā„
<button onClick={() => getState()}>Get State<buttonā„
none
option which you can skip saving data into session storage and this package can be use with React Native with this config set to none
as wellcreateStore(
{
yourDetail: { firstName: '', lastName: '' } // it's an object of your state
},
{
// when 'none' is used then state is not persisted
// when 'action' is used then state is saved to the storage after store action is completed
// when 'beforeUnload' is used then state is saved to storage before page unload and is restored
// after next page load and then storage is cleared
persist?: 'action' // onAction is default if not provided
},
);
huge thanks to @snax4a
const { actions } = useStateMachine()
// The following action will only update the store without flushing down a context re-render update
actions.updateStore({
test: 'data'
}, { skipRender: true })
type update: allow actions payload to be optional
export type ActionsOutput<
TCallback extends AnyCallback,
TActions extends AnyActions<TCallback>
> = {
- [K in keyof TActions]: (payload: Parameters<TActions[K]>[1]) => void;
+ [K in keyof TActions]: (payload?: Parameters<TActions[K]>[1]) => void;
};
fix: type issue with createStore
optional argument