[英]Redux - Reset state of subtree
What is the proper way to reset the subtree of a redux store? 重置Redux存储的子树的正确方法是什么? I'm not interested in resetting the entire redux store, but just the reducer subtree portion of it.
我对重置整个redux存储不感兴趣,而只是重置其中的reducer子树部分。
Here is an example snippet: 这是一个示例片段:
//initial state
const initialState = {
isFetching: false,
error: '',
page: 0
}
//reducer for suggestions store
export default function suggestions (state = initialState, action) {
switch (action.type) {
case FETCHING_SUGGESTIONS : {
return {
...state,
isFetching: true
}
}
case FETCHING_SUGGESTIONS_SUCCESS : {
return {
...state,
[action.suggestion.suggestionId] : action.suggestion
}
}
case FETCHING_SUGGESTIONS_ERROR : {
return {
...state,
isFetching: false,
error: "Error in fetching suggestions"
}
}
case CHANGE_PAGE : {
return {
...state,
page: action.page
}
}
case ADD_SUGGESTION : {
return {
...state,
[action.suggestion.suggestionId]: action.suggestion
}
}
case CLEAR_SUGGESTIONS : {
return {
initialState
}
}
default :
return state
}
}
I would think this would work, however whenever the CLEAR_SUGGESTIONS action is dispatched, I suddenly get undefined props in some of my components and the following error: warning.js:36 Warning: performUpdateIfNecessary: Unexpected batch number (current 161, pending 157) 我认为这可以解决问题,但是,每当分派CLEAR_SUGGESTIONS操作时,我的某些组件就会突然出现未定义的prop,并且出现以下错误:warning.js:36警告:performUpdateIfNecessary:意外的批号(当前161,待处理的157)
I'm not 100% confident I'm doing this correctly. 我不是100%自信我正确地做到了。 Can someone confirm if the issue is with my reducer, or somewhere along my components lifecycle methods?
有人可以确认问题出在我的减速器上还是组件生命周期方法中的某个地方?
Thanks! 谢谢!
The object you create will look like this: 您创建的对象将如下所示:
{
initialState: {
isFetching: false,
error: '',
page: 0
}
}
What you want is this: 您想要的是:
case CLEAR_SUGGESTIONS : {
return {
...initialState
}
}
You are accidentally nesting initialState under a key called 'initialState. 您不小心将initialState嵌套在称为'initialState的键下。 You just want:
您只想要:
case CLEAR_SUGGESTIONS : {
return initialState
}
There's no need to copy initialState, since store state is immutable. 由于存储状态是不可变的,因此无需复制initialState。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.