[英]react-navigation child component data to parent component function [react-navigation 5]
[i'm new in react-native.] [我是 react-native 的新手。]
how do i pass child component data to parent component function in react-navigation 5我如何将子组件数据传递给 react-navigation 中的父组件 function 5
suppose i navigate to child component from parent component parent.jsx by假设我从父组件parent.jsx导航到子组件
this.props.navigation.navigate('ChildComponentScreen');
i have a function in parent.jsx component我在parent.jsx组件中有一个 function
parentFunction = (data) => {
}
how i called parentFunction in parent.jsx from child.jsx component with data.我如何使用数据从child.jsx组件调用 parent.jsx 中的parentFunction 。
You can pass the function as a parameter to the next screen(ChildScreen) like below您可以将 function 作为参数传递给下一个屏幕(ChildScreen),如下所示
class HomeScreen extends React.Component {
sendData = (data) => {
alert(data);
};
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() =>
this.props.navigation.navigate('ChildComponent', {
sendData: this.sendData,
})
}
/>
</View>
);
}
}
And call it from the child screen like below并从下面的子屏幕调用它
class ChildScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details"
onPress={() => this.props.route.params.sendData('Details')}
/>
</View>
);
}
}
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.