我有這個非常簡單的組件,它連接到redux狀態並返回{fruit, vegetables}
。一切工作正常,但假設我在Component和I中有一個圖表,如果從API每次只接收更新的vegetable
,圖形將被重新創建。Redux mapStateToProps多次調用
這裏是我的組件:
const Products = ({ fruit, vegetable }) =>
<div className='Products'>
<div>{vegetable.map(....logic here)}</div>
<div>{Math.random()}</div> // this is to illustrate the component is rendering every time
<Graph>Here will be a chart or a graph with fruit</Graph> //but it gets re-rendered even though there is no new fruit
</div>
const mapStateToProps = (state) => {
return {
fruit: state.data.fruit,
vegetable: state.data.vegetable,
}
}
export default connect(mapStateToProps)(Products)
在我看來,每一次,不管其中規定更新它重新呈現整個組件。
有沒有辦法阻止?
問題不在於我使用一個無狀態組件 –
然後你可以使用一些更高階的組件來做這件事,就像這個純粹的幫助器重新構造一樣。它看起來像:'export default pure(MyStatelessComponent)'。 – VonD
我明白了。有沒有一個例子可以解決我的上述問題? –