ストアの状態(またはその一部)をローカルストレージに保存するために使用できるquite a few librariesがあります。
多分、一番人気のあるのはredux-persist
です。あなたはそれを使用するような何か行うことができます:
import { combineReducers } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/es/storage'
const modalReducer = (state = {}, action) => {
switch (action.type) {
case 'SHOW_MODAL':
return {
...state,
display: true,
lastShown: new Date()
}
case 'HIDE_MODAL':
return {
...state,
display: false
}
default:
return state
}
}
const modalPersistConfig = { key: 'modal', blacklist: ['display'], storage }
const rootReducer = combineReducers({
modal: persistReducer(modalPersistConfig, modalReducer)
// other reducers
})
const store = createStore(reducer)
persistStore(store)
を次に、あなたは、店舗(store.getState().modal.lastShown
)で日付を確認することができますし、それが今日の日付でなければSHOW_MODAL
アクションを派遣します。
私はredux-localStorageで遊んでいましたが、これは実装と保守が非常に簡単です。私はすぐにこれを試してみるつもりです。ありがとう:) –
私は確かにredux-localstorageも仕事に良いと思います。私は前にどちらかを実際に使っていないので、どうやってそれをどうやって聞くのが大好きです。 –