javascript - 超过最大更新深度。反应原生
问题描述
我正在尝试将值从一个组件传递到另一个组件。这是我的父组件。
<View>
{ this.state.errors ? <ErrorNotice content={this.state.content} /> : null}
</View>
这是我的子组件
constructor(props){
super(props);
this.state = {
errorMessage : "",
visibility: false
}
}
closeErrorNotice = ()=>{
this.setState({
visibility : false
})
}
render(){
return (
<View style={styles.errorContainer}>
<Text style={styles.errorText}>
{this.props.content.errorMessage}
</Text>
<Icon
name="x"
color="#454749"
size={25}
style={styles.closeButton}
onPress= {this.closeErrorNotice()}/>
</View>
)
}
但是我得到了超过最大更新深度。我找不到错误。任何人都可以找到错误。
解决方案
传递给时不要调用该函数onPress
。你得到一个无限循环,因为你正在调用更新状态的函数,这使得函数再次被调用。您需要传递函数定义,而不是调用函数的结果。
onPress= {this.closeErrorNotice}
推荐阅读
- ms-access - 在 MS Access 表单的文本框中添加值
- java - 当我更改附加到 ParallelCamera 的刻度的枢轴时,第一次更改刻度时,相机似乎会跳跃
- c# - 合并渐变层 - [C#]
- r - r - foreach 包返回错误信息
- postman - 是否有在 Postman 请求 URL 中使用环境变量的新语法?
- javascript - 一个函数产生两个不同的结果?回调地狱
- angular - 错误:无法匹配任何路由 URL Segment Angular 2
- java - 如何生成依赖项目的jar文件
- ruby-on-rails - 无法为我的 rails 应用程序添加表单
- python - 熊猫数据框绘图精度