How to update single value inside specific array item in redux

前端 未结 8 1306
隐瞒了意图╮
隐瞒了意图╮ 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:35

    In my case I did something like this, based on Luis's answer:

    ...State object...
    userInfo = {
    name: '...',
    ...
    }
    
    ...Reducer's code...
    case CHANGED_INFO:
    return {
      ...state,
      userInfo: {
        ...state.userInfo,
        // I'm sending the arguments like this: changeInfo({ id: e.target.id, value: e.target.value }) and use them as below in reducer!
        [action.data.id]: action.data.value,
      },
    };
    
    
    0 讨论(0)
  • 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;
      }
    };
    
    0 讨论(0)
提交回复
热议问题