我想將特定reducer的當前狀態保存到會話存儲中,而不必在每個操作中明確調用它。Redux reducer - on state change
const { handleActions } = require("redux-actions");
// How do i make this logic run on any state change without adding it to every action?
const onStateChange = (state) => sessionStorage.save(keys.myReducer, state);
const myReducer = handleActions
({
[Actions.action1]: (state, action) => (
update(state, {
prop1: {$set: action.payload},
})
),
[Actions.action2]: (state, action) => (
update(state, {
prop2: {$set: action.payload},
})
)
},
//****************INITIAL STATE***********************
{
prop1: [],
prop2: []
}
);
有沒有辦法趕上特定減速的狀態變化的事件嗎?
我知道store.subscribe
,但是這對整個商店,我說的是聽一個特定的減速變化
感謝
您是否嘗試過像[redux-localstorage](https://github.com/elgerlambert/redux-localstorage)或[redux-persist](https://github.com/rt2zz/redux-persist)這樣的庫?或者你是否正在處理這些庫無法解決的利基用例? – excalliburbd
@excalliburbd - 堅持狀態不是唯一的邏輯我想運行狀態改變,所以不幸的是這並不能解決問題 – Daniel