react-native - 如何停止在 setState 上渲染图像
问题描述
我有与计数器相关联的图像,并且基于计数器的增量或减量,计算完成并显示在底部的文本中。
问题是当我渲染时,图像会再次渲染并一次又一次地加载。我不想要。
如果我不渲染,文本将不会随着计算的数量更新。对于计数器,我使用的是 react-native-counter。
我已经尝试过 shouldcomponentupdate,但我只想停止图像渲染,其余的应该可以工作。
请指教。
export default class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<Header
backgroundColor="#25D366"
leftComponent={
<Icon
name="menu"
size={40}
color={"#fff000"}
onPress={() => this.props.navigation.openDrawer()}
/>
}
centerComponent={{
text: "Veg & Fruits",
style: {
color: "#ffffff",
fontSize: 25,
fontWeight: "bold",
},
}}
rightComponent={<Icon name="home" color={"#ff0000"} />}
></Header>
/// this is searchbar component,
<SearchBar
fontColor="#ffffff"
fontWeight="bold"
fontSize={20}
iconColor="#c6c6c6"
shadowColor="#ffffff"
cancelIconColor="#c6c6c6"
backgroundColor="#25D366"
placeholder="Search here"
onChangeText={(text) => {
this.setState({ photos: [] });
this.state.search = text;
this.filterList(this.state.search);
console.log("text changed");
}}
onPressCancel={(text) => {
text = "";
//this.filterList(text);
}}
onPress={(text) => {
console.log("rendering");
console.log("now text is: ", this.state.search);
}}
/>
/// in this view images are displayed using functions
<View>
<ScrollView
style={{
height: Dimensions.get("window").height - 200,
}}
>
<View
key={Date.now()}
style={{
flex: 1,
flexDirection: "column",
flexWrap: "wrap",
}}
>
{this.filterList(this.state.search)}
{this._renderImages()}
</View>
</ScrollView>
<CalcText tt={total_num} />
</View>
</div>
);
}
}
class CalcText extends Component {
constructor(props) {
super(props);
this.state = {
ta: 0,
};
}
shouldComponentUpdate(nextProps) {
console.log(nextProps.tt);
if (this.props.tt !== nextProps.tt) {
console.log("changed");
return true;
} else {
console.log("Not changed");
return false;
}
}
render() {
return (
<View style={{ height: 40, backgroundcolor: "ff0000" }}>
<Text style={{ fontSize: 26, fontWeight: "bold" }}>
Total : {this.props.tt}
</Text>
</View>
);
}
}
解决方案
您可以创建一个TextBox
组件并将其从ImageComponent
. 这样,图像就不会绑定到组件渲染文本的状态,您可以安全地更改TextBox
状态和防止ImageComponent
重新渲染的文本。
编辑:
Okei,现在我明白了。我认为你不可能这样做。
让我们将问题形式化:
<Parent>
<Images calc={functionToCalc} />
<CalcText totalAmount={this.state.totalAmount}/>
</Parent>
functionToCalc
在 in 中定义<Parent>
并更新父状态,例如:
const funcToCalc = () => {
// when condition occurs
this.setState({
totalAmount : computedAmount
})
}
的状态<Parent>
有:
this.state : {
totalAmount: none
}
<Images/>
每当您运行functionToCalc
update <Parent>
state 和 rerender时出现条件(buttonClick?)<CalcText>
。这里的问题是也<Images>
将再次重新渲染,因为所有父组件都将重新渲染。
这是在 React 中从兄弟姐妹传递信息的一种方式。
<Images>
只有当你想阻止重新渲染时,你才有可能。
- 用于集中状态的Redux或类似库
您将在 a 中移动计算的计算Store
并<CalcText/>
从那里读取它。<Images/>
组件将触发修改该状态的操作,但不会监听该状态,因此不会重新渲染。
推荐阅读
- c++ - 如何确保模板参数具有 value_type 并且是可迭代的?
- javascript - 当我点击 youtube 图标时,我会做什么,它会将我带到 www.google.com
- html - 为什么页脚没有归档全屏宽度 html/Bootstrap 5
- python - 使用 Python 反转数字中的位
- python - 我得到了 IndexError: Target 3 is out of bounds。在线损失中从 CNN 模块制作模型时出错 = loss_fn(outputs, labels)
- sql - 如何使用 postgres 10 将最新的 2 行加入到视图中
- flutter - flutter_admob 如何与 IOS swift 集成
- python - 将日期时间字符串转换为日期时间对象 python
- sql-server - 自动化 Azure SQL Server 数据库恢复
- angular - 模态确认后刷新屏幕(Angular)