@spyna/react-store
React app state management that uses a storage.
Install
Usage
View working code on CodeSandbox
Create store
You can pass the initial store value as the second argumento of the function createStore
.
Connect a component to the store
Using props.store.get('key')
Using props.key
You can pass an array of keys to the function withStore
to spread the keys of the store into the Component props
Set data in store
Read data from the store
Remove data from the store
Get all data from the store
Set multiple data in one shot
Using an object with the key
and the value
properties: {key: 'key', value: theValue}
Listen to store modification
You can listen to store modifications passing a callback function to the createStore
function config object.
The config property is: listener
.
In the following example, the store is initialized reading the localStorage
and every time the store is modified, its value is stored in the localStorage
.
const MY_STORE = 'my-store';
const initialValue = JSON.read(localStorage.getItem(MY_STORE) || "{}");
const config = {
listener: (state) => {
localStorage.setItem(MY_STORE, JSON.stringify(state))
}
}
export default createStore(App, initialValue, config)
Configuration
When creating the store with createStore
you can pass some options:
- initial store value
- custom store configuration
initial value
Default config
| Property | Type | default | meaning |
| --- | --- | --- |
| listener | function | (state) => {}
does nothing | A callback function that is called after the methods: set
, setAll
, remove
. The function accepts the new store
value as parameter. |
| proxyStore | boolean | Whether or not to use a Proxy
to lock the store object. In some environment the Proxy
object is not available, for example when using Facebook hermes, in these cases you can set this property to false
. Keep in mid that if you set this option to false, store
will not be protected against modifications. |
Contributing
Pull request and contributions are welcome.
The develop
branch is the one where you want to develope your changes.
The master
branch is the source code of the current release.
The gh-pages
branch is mainteined by CI and contains the documentation and example. You don't need to use it.
- Add test for your changes
- Add documentation and examples of your changes under the folder
example
- Run
yarn prettier
ornpm run prettier
to format the source of the project - Thank you
Developmnent
Run yarn start
to compile to source code.
To test changes run cd example && yarn start