javascript - 如何将 redux 状态返回到初始状态?

标签 javascript state redux

我在弄清楚这一点时遇到了非常困难的事情,本质上我是在尝试将状态设置为初始状态,到目前为止我已经尝试过:

// -- 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;
  }
}

这只是将初始状态添加到现有状态


case ACTIONS.RESET_STATE:
      return { ...state, state = INITIAL_STATE }

返回错误


case ACTIONS.RESET_STATE:
      return { ...state, state: INITIAL_STATE }

这是将初始状态添加到现有的一个增益中


case ACTIONS.RESET_STATE:
      return { ...state, search: { listings:[] }, listings: [] }

这行得通,但我开始收到奇怪的突变错误。

最佳答案

Anders 提出的解决方案是正确的,但在不可变方面存在潜在问题。这总是生成新对象。

case ACTIONS.RESET_STATE:
    return { ...INITIAL_STATE };

关于javascript - 如何将 redux 状态返回到初始状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477202/

相关文章:

php - 在查询字符串中使用长哈希来识别动态文档的安全性

C++ 监视变量的更改值

javascript - jQuery FullCalendar 未在 Bootstrap 选项卡中呈现

javascript - Angular 使用 html 文件的模板路径

Android自定义复合 View 保存和恢复状态不起作用

reactjs - 如何使用 redux thunk 在成功或错误后重定向?

javascript - Actions 必须是普通对象 React 和 Redux 中的错误

javascript - 将正则表达式保存到 Redux 存储中?

javascript - 父类(super class)化stream.Duplex的问题

android - 一起滚动两个 Lazy Scrollers