Decouple your storage more and better.
npm i @hacknlove/substore
All key
can be 'deep.dotted.key'
.
see @hacknlove/deepobject
Returns the substore at key
.
If there is a substore at that key
, it returns that substore. If not, it creates a new one.
The substore methods mimic the store methods.
const store = require('@hacknlove/reduxplus')
require('@hacknlove/substore')
const MySubStore1 = store.subStore('someKey')
assert(MySubstore1.i === 1)
const MySubStore2 = store.subStore('someKey')
assert(MySubstore1.i === 2)
assert(MySubStore1 === MySubStore2)
const MySubStore3 = store.subStore('otherKey')
assert(MySubStore1 !== MySubStore3)
assert(MySubstore3.i === 1)
Returns the state of the substore
Set a reducer to that substore, to process the actions dispatched in the substore.
Dispatch an action in the substore, that will be processed by the reducers of the substore.
React hook that refresh when the state of the substore changes.
React hook that refresh when the substore's state's value at key
changes.
It set the store's state.
If replace === true
, it replaces the old state with the new one.
If replace !== true
, it replaces the old state with the merge of the new one in the old one.
Returns a unsubscribe function.
The callback is called each time the substore's state changes, until unsubscribe funcion is called.
Returns a unsubscribe function.
The callback is called each time the substore's state's value at key
changes, until unsubscribe funcion is called.
If this is the last substore at its key
, it removes all subscriptions, all reducers, stop all useRedux hooks and cleans all substore's substores. After that every substore method will throw new Error('subStore cleaned')
If this is the last substore at its key
, it all, including the state
it returns a new substore at ${substore.key}.${key}
that will be cleaned when the parent substore is cleaned.
You cannot set a middleware to a substore.
You will see the subStore actions as ºkey/type
const substore = store.subStore('foo.bar')
substore.dispatch({
type: 'buz',
some: {
more: 'things'
}
})
/*
store.dispatch({
type: 'ºfoo.bar/buz',
key: 'foo.bar'
subAction: {
type: 'buz',
some: {
more: 'things'
}
}
})
*/