【问题标题】:Updating nested redux state更新嵌套的 redux 状态
【发布时间】:2017-01-06 22:51:44
【问题描述】:

我有一个 reducer,它接收一个带有我需要更新状态的有效负载的操作。问题是我需要在状态中更新的数据是嵌套数据。 我在下面添加了我的减速器,并附上了一些评论以及到目前为止我尝试做的事情。

export default function(state=data, action){
  switch (action.type) {
    case 'UPDATE_CONTACT_INFO':
      let appointment = state[action.appointmentIndex]; // This is the appointment that needs to be updated 
      appointment.notification.contactInfo = action.payload; // this is the data that needs to be updated with the payload. I tried updating it like this but then not sure how to add it to the state. 
      return state; // Somehow need to update the state with the new state
      break;
    default:
    return state;
  }
}

下面是我的初始数据结构,我将其作为默认状态传递给 reducer。

 data = [
        {
        date: 'Friday, January 6',
        time: '4:00 PM-5:00 PM',
        notification:
          {
            contactInfo: [
              {
                displayMethod:"Phone Call",
                method:"Phone",
                value:"3473686552"
              },
              {
                displayMethod:"Email",
                method:"Email",
                value:"memedoe@gmail.com"
              }
            ]
          }
      },
      {
        date: 'Saturday, January 7',
        time: '2:00 PM-6:00 PM',
        notification:
          {
            contactInfo: [
              {
                displayMethod:"Phone Call",
                method:"Phone",
                value:"2123686552"
              },
              {
                displayMethod:"Email",
                method:"Email",
                value:"johndoe@gmail.com"
              }
            ]
          }
      }
    ];

reducer 数据中的 action.payload 与其中一个约会中的 contactInfo 数组的结构相同。 [Object, Object]

【问题讨论】:

    标签: reactjs react-redux


    【解决方案1】:

    使用 redux,您永远不会更新状态。您必须返回一个新的状态对象,其中包含更新的数据。

    为此,您需要使用 Object.assign() 或 ES6 扩展运算符 {...}

    我提供了两者的链接: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator

    在此处阅读减速器: http://redux.js.org/docs/basics/Reducers.html

    特别注意我们不改变状态点。

    【讨论】:

      【解决方案2】:

      所有此类问题都可以使用react-addons-update 包解决。阅读here

      这种情况可以这样解决:

      export default function(state=data, action){
        switch (action.type) {
          case 'UPDATE_CONTACT_INFO':
            return update(state, {[action.appointmentIndex]:{notification: {contactInfo: {$set: action.payload}}}});
          default:
          return state;
        }
      }
      

      【讨论】:

      • 哇,这很有效,但它的工作原理令人困惑。我已经阅读了文档,但仍然没有了解这部分在做什么。 {[action.appointmentIndex]:{notification: {contactInfo: {$set: action.payload}}}}
      • [action.appointmentIndex] 只是一个数字,所以它如何能够根据上面的内容知道要获取数组中的哪个对象
      • 当它工作时,你可以用勾号标记它(这将是我的第一个,你知道的)。它是如何工作的?嗯,对我来说,API 很清楚 - 当您的状态是一个数组时,您可以通过将其在数组中的索引声明为约定来进入其中的一个元素。是的,这是一个数字,但这已经足够了——数组的索引是一个数字,这很清楚。
      • 如果我有state = [1,2,3,4] 并且想将3 更改为33,我的代码将是update(state, {[2]: 33})
      • 感谢您的解释
      【解决方案3】:

      您需要使用 object.assign 来更改商店中的数据

      const newstateobject = Object.assign({}, state[someIndex], {notification: Object.assign({}, state[someindex].notification, {contactInfo: action.payload})});
      
      return Object.assign({}, state, {[someindex]: newstateobject);
      

      【讨论】:

      • 我正在更新该州的特定约会,而不是所有约会。你有 state.appointment 那是什么?
      • 我改了答案,没看到我们在处理数组。
      • state.appointment 注意到它甚至不是数据结构的一部分,如您所见。我想你的意思是 state[action.appointmentIndex] 来获得特定的约会?
      • 是的。在这里深夜;)我希望我至少能让你朝着正确的方向前进:)现在太累了,无法思考;)
      • 让约会 = state[action.appointmentIndex];让contactInfo =约会.通知.contactInfo;常量约会 = Object.assign({}, 约会, {contactInfo: Object.assign({}, contactInfo, action.payload})); return Object.assign({}, state, {state.appointment: 约会});
      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2019-01-28
      • 2020-11-17
      • 2021-03-15
      相关资源
      最近更新 更多