【问题标题】:change in props not causing re-render in react - redux app道具的变化不会导致反应中的重新渲染 - redux app
【发布时间】:2017-10-11 14:26:48
【问题描述】:

我有一个 react -redux 应用程序。它工作正常。但是,当使用调用 props.dispatch() 更新某个特定组件中的道具时,使用此道具的其他组件不会重新渲染。我认为 redux 无法识别该特定道具的变化。 对象是

[
   {lunch: 0, dinner: 12, breakfast: 0},
   {lunch: 1, dinner: 2, breakfast: 0}, 
   {lunch: 0, dinner: 12, breakfast: 10}
]

这样的变化

[
   {lunch: 0, dinner: 1, breakfast: 0},
   {lunch: 1, dinner: 2, breakfast: 0}, 
   {lunch: 0, dinner: 1, breakfast: 10}
]

所以对象只有轻微的变化。但这应该会导致使用此道具的组件重新渲染。 但这种情况并非如此。我已经检查了减速器,那里的对象正在改变。 其他道具的更改也需要重新渲染。仅在此对象的情况下。它不是重新渲染。 有什么建议么? 编辑:- 在组件中-

this.props.dispatch({type: actions.SAVE_SELECTED_MEAL, selectedMeal: selected});

在减速器中——

case actions.SAVE_SELECTED_MEAL: return { ...state, selectedMeal: action.selectedMeal // [{lunch: id, dinner: 12, breakfast: 0}] }

【问题讨论】:

  • 您需要发布更多代码,特别是您的问题中使用的减速器和组件。
  • 我找到了答案。

标签: javascript reactjs redux react-redux


【解决方案1】:

我在这个 redux 问题https://github.com/reactjs/redux/issues/585 中找到了答案 以防有人遇到类似的问题。 实际上,我在我的组件中改变对象我将代码从const selected = this.state.selectedMeal; 更改为const selected = {...this.state.selectedMeal}; 现在它工作正常。

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2022-01-22
    • 1970-01-01
    • 2018-11-16
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多