React native 将道具从模态传递到主屏幕
我正在使用React-Native和React-Native模式 我想做的是在模式中输入任务详细信息文本,然后在隐藏模式上单击将输入的文本传递到主屏幕 试图将文本作为状态属性传递-但未成功 这是我的情态:React native 将道具从模态传递到主屏幕,react-native,modal-dialog,react-props,React Native,Modal Dialog,React Props,我正在使用React-Native和React-Native模式 我想做的是在模式中输入任务详细信息文本,然后在隐藏模式上单击将输入的文本传递到主屏幕 试图将文本作为状态属性传递-但未成功 这是我的情态: export default class List extends Component { constructor() { super(); } state = { modalVisible: false, taskDetails: '' }; se
export default class List extends Component {
constructor() {
super();
}
state = {
modalVisible: false,
taskDetails: ''
};
setModalVisible(visible) {
this.setState({ modalVisible: visible });
}
render() {
const textInput = {
backgroundColor: '#a2a2a2'
}
const styles = {
display: 'flex',
justifyContent: 'center',
padding: 5,
height: 50,
width: '100%',
backgroundColor: '#fbfbfb',
borderColor: '#a2a2a2',
borderWidth: 1,
borderStyle: 'solid'
}
return (
<View>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {
}}
>
<View>
<View>
<View>
<Text>Service name</Text>
<TextInput style={textInput} onChangeText={taskDetails => this.setState({ taskDetails })} value={this.state.taskDetails} />
</View>
<TouchableHighlight
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<TouchableHighlight
onPress={() => {
this.setModalVisible(true);
}}>
<Text>Show Modal</Text>
</TouchableHighlight>
</View>
)
}
}
如果您的主屏幕是另一个屏幕,您可以通过导航道具的setParams传递所需数据,如下所示:
但是,如果主屏幕是该组件的父级,则可以将onChangeText函数作为prop传递,然后从那里设置state或调用其他函数。在构造函数中:this.state={modalVisible:false,taskDetails:};为什么一次将modalVisible标记为false,而另一次标记为可见?!
class ProfileScreen extends React.Component {
render() {
return (
<Button
onPress={() => this.props.navigation.setParams({ name: 'Lucy' })}
title="Set title name to 'Lucy'"
/>
);
}
}
const name = this.props.navigation.getParam('name', 'Lucy');