How to update single value inside specific array item in redux

前端 未结 8 1424
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  猫巷女王i
    2020-11-28 02:44

    This is how I did it for one of my projects:

    const markdownSaveActionCreator = (newMarkdownLocation, newMarkdownToSave) => ({
      type: MARKDOWN_SAVE,
      saveLocation: newMarkdownLocation,
      savedMarkdownInLocation: newMarkdownToSave  
    });
    
    const markdownSaveReducer = (state = MARKDOWN_SAVED_ARRAY_DEFAULT, action) => {
      let objTemp = {
        saveLocation: action.saveLocation, 
        savedMarkdownInLocation: action.savedMarkdownInLocation
      };
    
      switch(action.type) {
        case MARKDOWN_SAVE:
          return( 
            state.map(i => {
              if (i.saveLocation === objTemp.saveLocation) {
                return Object.assign({}, i, objTemp);
              }
              return i;
            })
          );
        default:
          return state;
      }
    };
    

提交回复
热议问题