-
Notifications
You must be signed in to change notification settings - Fork 1
Persistant
One of most geat features of Wallant is persistant state, it can't contains (or shouldn't) big quantity of information, but it's useful to save user session, tokens, settings, preferences, etc. And it's so easy.
const store = new Wallant({
persistant: true, // <- add this line
state: { ... },
actions: { ... }
... more stuffs
})For next, if you add data to state
store.setState({
user: {
name: 'Eliseo',
token: '2qaszxcfgt654redfvbnhji87yhjkio'
}
})
// and commit state
store.commit() // <- this is the dispatcher for persist current statecommit method save all state (except your ignore nodes), and this state is recovered automatically later, when store is created again, it uses AsyncStorage, it's a good idea keep a small ammount of data.
You can dispatch commit just on setState (ss)
store.ss({
...data
}).commit() // <- like thisRestauration of data is asyncrhorous, app starts, make render, an later data is restored, for solve this problem we create hooks, onRestored method for help you to control data restauration flow. For example, if we save a token and check for them on app startup, we will get a null.
constructor(props) {
super(props)
store.state.token !== null // false
store.onRestored(() => {
// this can use restores state
store.state.token !== null // true
})
}It's great, but sometimes, we wont (and shouldn't) save all our data, this is the reason why we create ignore nodes.
const store = new Wallant({
persistant: true,
ignore: ['nodeOne', 'nodeTwo'],
state: {
nodeOne: [], // <- this node
token: 'xcvbnjiu7654ewsdfgyui90',
nodeTwo: [], // <- and this node will be ignored on commit
nodeThree: {},
nodeFour: ''
}
})