Reactjs React native重新渲染所有组件,包括子组件
我想在redux状态改变时重新加载一些特定的子组件,以便解决这个问题 这是我的密码:Reactjs React native重新渲染所有组件,包括子组件,reactjs,react-native,redux,react-native-android,Reactjs,React Native,Redux,React Native Android,我想在redux状态改变时重新加载一些特定的子组件,以便解决这个问题 这是我的密码: <ItemMiniCard itemName={prop.item} price={prop.price} onPressAdd={() => { this.props.addCartItem(prop.id) }} onPressRemove={() => { this.props.removeCartItem(prop.id) }} /> {this.p
<ItemMiniCard
itemName={prop.item}
price={prop.price}
onPressAdd={() => { this.props.addCartItem(prop.id) }}
onPressRemove={() => { this.props.removeCartItem(prop.id) }}
/>
{this.props.addCartItem(prop.id)}
onPressRemove={()=>{this.props.RemoveCarItem(prop.id)}
/>
在此项中,迷你卡还有一些其他子组件
<View style={{ flex: 1, marginLeft: 5 }}>
<Text numberOfLines={1} style={[styles.itemText, { padding: 0 }]}>{itemName}</Text>
<Text numberOfLines={1} style={[styles.descriptionText, { padding: 0 }]}>Delicius food rice and curry tasty food</Text>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', }}>
<Text numberOfLines={1} style={[styles.priceTextBold, { padding: 0 }]}>LKR {price} {count} </Text>
{count == 0 &&
<Cbutton
title='Add +'
style={{ width: 52, height: 22 }}
onPress={onPressAdd}
/>
}
{count > 0 &&
<View style={{backgroundColor:'white',flexDirection:'row'}}>
<Cbutton
title='+'
style={{ width: 30, height: 20 }}
onPress={onPressAdd}
/>
<View style={{marginLeft:3,marginRight:3}}>
<Text>{count}</Text>
</View>
<Cbutton
title='-'
style={{ width: 30, height: 20 }}
onPress={onPressRemove}
/>
</View>
}
</View>
</View>
</View>
{itemName}
美味的食物米饭和咖喱美味的食物
LKR{price}{count}
{count==0&&
}
{计数>0&&
{count}
}
在这里,当我向购物车产品添加获取重新渲染的整个组件时,如何防止重新渲染整个组件为了防止获取重新渲染的整个组件,您可以在react中使用PureComponent
class Greeting extends React.PureComponent {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
类问候语扩展了React.PureComponent{
render(){
返回Hello,{this.props.name};
}
}
默认情况下,在单个组件中渲染会导致其所有子组件也进行渲染。即使他们的道具没有改变,但是React.PureComponent实现了shouldComponentUpdate(),并进行了简单的道具和状态比较
您可以使用
shouldComponentUpdate
处理道具或状态来重新渲染组件
如果仅在计数
或价格
更改时才需要重新渲染屏幕,则示例:
shouldComponentUpdate(nextProps, nextState){
if (nextProps.price !== this.props.price ||
nextProps.count !== this.props.count)
return true;
return false
}
如果在列表中使用
ItemMiniCard
,您是否尝试过设置key
?我没有在列表中添加ItemMiniCard如果我在列表中添加ItemMiniCard,这个问题可能会解决吗?当您有相同类型的子项时,实际上添加key
有助于在数据未更改的情况下不重新提交子项。并且您可以使用React的shouldComponentUpdate
来指定您需要更新的时间要渲染的组件。