如何将Redux状态恢复为初始状态? [英] How to return redux state to initial state?
本文介绍了如何将Redux状态恢复为初始状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难解决这个问题,本质上是我试图将状态设置为初始状态,到目前为止,我已经尝试过:
I'm having surprisingly difficult time figuring this out, essentially I'm trying to set state to initial state, so far I tried:
// -- Initial state ------------------------------------------------------------
const INITIAL_STATE = {
search: {
listings: []
},
listings: []
}
// -- Story structure for story editor -----------------------------------------
export default function(state = INITIAL_STATE, action) {
switch(action.type) {
case ACTIONS.RESET_STATE:
return { ...state, INITIAL_STATE }
default:
return state;
}
}
这只是将初始状态添加到现有状态
this just adds initial state to existing one
case ACTIONS.RESET_STATE:
return { ...state, state = INITIAL_STATE }
这将返回错误
case ACTIONS.RESET_STATE:
return { ...state, state: INITIAL_STATE }
这是将初始状态添加到现有的一个增益中
this is adding initial state to existing one gain
case ACTIONS.RESET_STATE:
return { ...state, search: { listings:[] }, listings: [] }
这可行,但是我开始出现奇怪的突变错误.
This works, but I start getting weird mutation errors.
推荐答案
如果只想完全重置状态,只需返回INITIAL_STATE
的 value :
If you simply want to reset state completely, just return the value of INITIAL_STATE
:
export default function(state = INITIAL_STATE, action) {
switch(action.type) {
case ACTIONS.RESET_STATE:
return {
search: {
listings: []
},
listings: []
};
default:
return state;
}
}
如果要将INITIAL_STATE
放在一个位置.将初始状态创建者更改为函数:
If you want to keep the INITIAL_STATE
in a single place. Change the initial state creator to a function:
function get_INITIAL_STATE => {
return { search: {
listings: []
},
listings: []
}
}
export default function(state = get_INITIAL_STATE(), action) {
switch(action.type) {
case ACTIONS.RESET_STATE:
return get_INITIAL_STATE();
default:
return state;
}
}
这篇关于如何将Redux状态恢复为初始状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文