[英]change in props not causing re-render in react - redux app
我有一個react -redux應用程序。 一切正常。 但是,當使用調用props.dispatch()更新一個特定組件中的props時,正在使用該props的其他組件不會重新呈現。 我認為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}] }
我在這個redux問題https://github.com/reactjs/redux/issues/585中找到了答案,以防有人遇到類似問題。 實際上,我正在更改組件中的對象,因此我將代碼從const selected = this.state.selectedMeal;
更改為const selected = this.state.selectedMeal;
const selected = {...this.state.selectedMeal};
現在它的工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.