react-persist-context
v1.0.7
Published
Persisting state management library built on top of React context
Downloads
4
Readme
react-persist-context
Simple React state management library built on top of useContext and useReducer with data persistence.
Installation
npm i react-persist-context
Usage
Initialization
Wrap the main entry file with PersistentContextProvider.
import { PersistentContextProvider } from 'react-persist-context'
// code before returning...
return (
<PersistentContextProvider store={store}>
<EntryComponent {...props} />
</PersistentContextProvider>
)
The store is an object value consisting of state and reducer (the same basic structure from redux).
const store = {
state: yourInitialState
reducer: yourReducer
}
Accessing persisted context
After initializing your provider, you can now access the persisted context using usePersistedContext which returns { state, dispatch }
// component who is accessing
import { usePersistedContext } from 'react-persist-context'
const CompononentWhoIsUsing = () => {
const { state, dispatch } = usePersistedContext()
/* rest of the code*/
return (...)
}
Persistence
The data persistence uses cookies and by default in session.
License
ISC