import {createStore} from 'state-pool';
const store = createStore();
// You can create as many stores as you want
Integrated useState and useReducer into a store, this allows us to consume our state with store.useState & store.useReducer
Introduced a generic API for implementing state persistence in any storage
store.persist({
saveState: function(key, value, isInitialSet){/*your code to save state */},
loadState: function(key){/*your code to load state */},
removeState: function(key){/*your code to remove state */},
clear: function(){/*your code to clear storage */}
})
Managing state outside react component(reading and writing state outside react component)
// Reading state outside of React component
store.getState(key).getValue() // Reading value of a global state from a store
globalState.getValue() // Reading value of a global state
// Listening to store changes
const unsubscribe = store.subscribe(function(key: String, value: Any){
// key is the key for a global state that has changed
// value is the new value of a global state
})
// You can unsubscribe by calling the result
unsubscribe();
//If you want to listen to a single global state in a store you can use
const unsubscribe = store.getState(key).subscribe(function(value){
// value is the new value of a global state
})
// For a plain global state
const unsubscribe = globalState.subscribe(function(value){
// value is the new value of a global state
})
// You can unsubscribe by calling the result
unsubscribe();
// Writing to a global state in a store
store.getState(key).updateValue(value => {
// Do your updates here e.g value.name = "Yezy"
})
// Writing to a plain global state
globalState.updateValue(value => {
// Do your updates here e.g value.name = "Yezy"
})
Breaking Changes
We can no longer import a store from state-pool, we need to import createStore instead.
We can no longer pass a key parameter, persist and default configurations in useGlobalState and useGlobalStateReducer hooks, The key parameter, persist and default configurations are now accepted by store.useState and store.useReducer
store.LOCAL_STORAGE_UPDATE_DEBOUNCE_TIME is no longer used, since we have empowered users to implement state persistence by themselves
useLocalState is removed, since our focus is on managing global state
New Features
Added the ability to create a store
Integrated
useState
anduseReducer
into a store, this allows us to consume our state withstore.useState
&store.useReducer
Introduced a generic API for implementing state persistence in any storage
Introduced state typing
Managing state outside react component(reading and writing state outside react component)
Breaking Changes
store
fromstate-pool
, we need to importcreateStore
instead.useGlobalState
anduseGlobalStateReducer
hooks, The key parameter, persist and default configurations are now accepted bystore.useState
andstore.useReducer
store.LOCAL_STORAGE_UPDATE_DEBOUNCE_TIME
is no longer used, since we have empowered users to implement state persistence by themselvesuseLocalState
is removed, since our focus is on managing global state