【发布时间】:2018-11-19 09:44:11
【问题描述】:
我在 react native 中创建了模态。我现在在右上角添加了过滤器选项图标,当用户点击它应该打开模式。我怎样才能做到这一点 ?我在 navigation.js 中添加了选项图标,但现在如何将它与模态组件连接?
setModalVisible 下面的代码在 filteroptions.js 中可用,而在 navigation.js 中不可用
代码:
navigation.js:
Updates: {
screen: UpdatesScreen,
navigationOptions: ({ navigation }) => ({
headerTitle: 'Myapp',
headerRight:<TouchableOpacity onPress={() => {this.setModalVisible(true);}}>
<MenuIcon style={{paddingLeft: 10, paddingRight: 15}} name="md-options" size={25} color="white"/>
</TouchableOpacity>,
})
},
filteroptions.js:
import React, {Component} from 'react';
import {Modal, Text, TouchableHighlight, View, Alert} from 'react-native';
export default class FilteroptionsModel extends Component {
state = {
modalVisible: false,
};
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
render() {
return (
<View style={{marginTop: 22}}>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View style={{marginTop: 22}}>
<View>
<TouchableHighlight
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
</View>
);
}
}
当用户点击右上角的过滤器按钮时(见截图),他应该能够在屏幕上看到框模式:
【问题讨论】:
标签: javascript reactjs react-native