How to update single value inside specific array item in redux

前端 未结 8 1391
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 01:55

I have an issue where re-rendering of state causes ui issues and was suggested to only update specific value inside my reducer to reduce amount of re-rendering on a page.

8条回答
  •  时光取名叫无心
    2020-11-28 02:25

    You could use the React Immutability helpers

    import update from 'react-addons-update';
    
    // ...    
    
    case 'SOME_ACTION':
      return update(state, { 
        contents: { 
          1: {
            text: {$set: action.payload}
          }
        }
      });
    

    Although I would imagine you'd probably be doing something more like this?

    case 'SOME_ACTION':
      return update(state, { 
        contents: { 
          [action.id]: {
            text: {$set: action.payload}
          }
        }
      });
    

提交回复
热议问题