react-native - 如何通过具有状态的视图传递函数?
问题描述
需要你的帮助。
当我尝试通过不同的视图传递一个函数(其目的是关闭一个模式)时,我的代码遇到了问题。
父视图包含一个在单击时打开模式的按钮,子视图包含模式内容,包括应该关闭它的可触摸内容。关闭模式功能在父视图中。
这是代码:
//Button Function in the parent view (turn the state to false to hide modal)
closeEventParticipeModal() {
this.setState({showModalParticipeEvent: false});
}
//The function that contains the modal and shows it if the visible is true thanks to state.
renderParticipeModal() {
for (let i = 0; i < this.state.userProfile['events_participes'].length; i++) {
if (this.state.modalEventParticipeSelected == i) {
const event = this.state.userProfile['events_participes'][i];
return(
<EventParticipeModal
visible={this.state.showModalParticipeEvent}
event={event}
closebuttonFunction={this.closeEventParticipeModal()}
/>
);
}
}
//Here is the code that contains my modal view including a touchable that should close the modal when clicking on it.
export default class EventParticipeModal extends React.Component{
displayEventParticipeModal() {
return(
<Modal
animationType='slide'
transparent={true}
visible={this.props.visible}>
<TouchableOpacity style={{height: '15%'}}
onPress={() => {this.props.closebuttonFunction}}/>
<View style={{flex: 3, marginHorizontal: '3%', backgroundColor: 'white', borderRadius: 20, borderWidth: 0.2}}/>
</Modal>
);
}
render(){
return(
<View style={styles.touchableContainer}>
{this.displayEventParticipeModal()}
</View>
);
//My constructor is as followed
constructor(props) {
super(props);
this.state = {
backgroundColor: 'blue',
showModalParticipeEvent:false,
event_created:true,
events: [],
isLoading: true,
modalEventParticipeSelected:-1,
};
this.closeEventParticipeModal = this.closeEventParticipeModal.bind(this);
}
当我单击可触摸时,什么也没有发生。我尝试了很多东西,但遇到了各种错误,例如“超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。React 限制嵌套更新的数量以防止无限循环""
先感谢您 :)
解决方案
推荐阅读
- kotlin - 从列表中收集偶数到奇数元素以在 Kotlin 中映射的惯用方式
- node.js - 模块的后安装在子模块的后安装之前执行
- html - 使用 sjPlot::tab_df() 创建 HTML 数据框输出时,R 行名称被索引而不是打印
- php - 尝试使用 PHPMailer 发送大量电子邮件
- powerbi - 如何在 Power BI 中获得表格下方的视觉效果(需要最后一列的想法)
- google-sheets - 如何编写将数字编码为字母的公式?
- flask - Flask 在 POST 请求上重置连接,主体较大
- amazon-web-services - 如何在不同的 ECS 任务之间共享 EFS 并托管在不同的实例中
- realm - 从应用程序的 RealmObject 中嵌套库中的 RealmObject
- database - 用于时间序列数据的 Cassandra 数据模型以查询最后一小时数据