代码之家  ›  专栏  ›  技术社区  ›  user1008636

Redux:一次点击按钮时道具不变

  •  0
  • user1008636  · 技术社区  · 7 年前

    一个按钮的onClick将调用一个函数:

    onClick = {() => props.myFunction()}
    

    myFunction 将返回值100,键为' MyProperty

    我的第二个按钮只显示 我的财产

    onClick = {() => alert(props.MyProperty)}
    

    我连接了两个 我的函数 我的财产 到组件。但是当我点击第一个按钮,然后点击第二个按钮时,它会显示初始值 我的财产 ,并且从不更新“100”值。

    我的函数 {type: "MY_FUNCTION", MyProperty: 100}

    在我的减速机里我做到了:

    case "MY_FUNCTION":
    return {
     MyProperty: action.MyProperty,
     ...state
    }
    

    我会做错什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   varoons    7 年前
    state = { x: 1 }
    
    { x: 2 , ...state } ====> { x: 1 }  // x will never be updated as it gets replaced by what was in the previous state
    { ...state, x: 2  } ====> { x: 2 }
    

    https://dmitripavlutin.com/object-rest-spread-properties-javascript/