【发布时间】:2019-07-30 10:15:14
【问题描述】:
我在 react-native 中使用 Modal 展示一个组件。所以在子组件(在父组件顶部显示为模态的组件)中,我试图更新父组件的状态变量,但它会引发错误,例如“'changeModalVisibility' is missing in props validation”。
请帮我解决这个问题。
相关代码如下:
// Parent Class in render
<SafeAreaView style={styles.container}>
<Modal
animationType="slide"
transparent={false}
visible={this.state.isModalVisible}
onRequestClose={() => { this.changeModalVisibility(false) }}
>
<ChildClass
changeModalVisibility={this.changeModalVisibility}
/>
</Modal>
</SafeAreaView>
// Outside Render function
changeModalVisibility = (bool) => {
this.setState({ isModalVisible: visible });
}
// In Child Class
<TouchableHighlight
underlayColor="none"
onPress={this.props.closeButtonTapped}
style={{ alignItems: 'center', justifyContent: 'center', }}
>
<Text style={{
color: 'white',
marginTop: 10,
marginLeft: 20,
fontWeight: '600',
fontSize: 18,
}}
>Close
</Text>
</TouchableHighlight>
closeButtonTapped() {
this.props.changeModalVisibility //"**'changeModalVisibility' is missing in props validation**"
}
【问题讨论】:
-
这是issue with your prop-types,不是功能错误。
-
在closeButtonTapped()方法的末尾添加大括号()方法如
this.props.changeModalVisibility() -
@KaushikRadadiya:它的抛出错误“期望一个赋值或函数调用,而是看到一个表达式。道具验证中缺少 &”
-
您是否尝试过通过函数调用传递参数?像这样 this.props.changeModalVisibility(false)
-
@KaushikRadadiya:我试过了,效果很好,谢谢兄弟。
标签: react-native react-native-android react-native-ios react-native-navigation