代码之家  ›  专栏  ›  技术社区  ›  me-me

使用展开缩小存储形状

  •  0
  • me-me  · 技术社区  · 6 年前

    目前,我的国家正在超越自己。 我正在尝试这样做,这样它就不会覆盖数据数组。 我需要访问特定的数据索引。数据数组get中的对象是根据组件步骤添加的。但是,当更新发生时,我需要在不改变数据数组的特定索引的情况下进行传播。我怎么能用我得到的来做这个呢?我知道我很亲近。

    const initialState = {
          index: 0,
          data: [{}],
        }; 
    
    
    
    const reducer = (state = initialState, action) => {
      switch (action.type) {
      case UPDATE: {
          return {
            ...state,
            data: [{
                ...state.data[state.count],
                [action.name]: { value: action.value },
              }],
          };
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrews Gyamfi    6 年前

    你是压倒性的 state.data 随时更新。你要找的是这样的东西:

    const initialState = {
        index: 0,
        data: [{}],
    };
    
    const reducer = (state = initialState, action) => {
        switch (action.type) {
            case UPDATE: {
                return {
                    ...state,
                    data: state.data.map((value, index) => {
                        if (index === state.count) {
                            return {
                                ...state.data[state.count],
                                [action.name]: { value: action.value },
                            }
                        }
                        return value;
                    })
                };
            }
        }
    }