简体   繁体   English

如何借助 goBack() 发送前一屏幕的数据?

[英]How to send data in previous screen with help of goBack()?

How to send data in previous screen with help of goBack()?如何借助 goBack() 发送前一屏幕的数据?

I tried already. For ex: this.props.navigation.navigate('Login', {name: this.state.name})

If you simply want to pass data to the previous screen then the below works:如果您只是想将数据传递到上一个屏幕,则可以使用以下方法:

Parent screen:父屏幕:

const Parent = () => {
  const [data, setData] = useState("");
  let navigation = useNavigation();

  const onPress = () => {
    navigation.navigate('Second', { getData: (x)=>setData(x) }); //<---here you have to pass callback function
  };
  return (
    <View style={{ flex: 1 }}>
      <Button
        onPress={onPress}
        title="Add title here"
        color="#841584"
        disabled={false}
      />
      <Text>{data}</Text>
    </View>
  );
};

Child screen:儿童屏幕:

const Child = ({ route }) => {  //<------here need to take route 
  let navigation = useNavigation();
  const onPress = () => {
    let data = "John";
    route.params.getData(data);   //<-----this way to update.
    navigation.goBack();
  };
  return (
    <View style={{ flex: 1 }}>
      <Button
        onPress={onPress}
        title="Add title here"
        color="#841584"
        disabled={false}
      />
    </View>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 goBack()时如何刷新上一个屏幕 - How to refresh the previous screen when I goBack() 在 goBack() 上刷新上一个屏幕 - Refresh previous screen on goBack() 使用navigation.goBack()时如何将数据发送回上一个屏幕? - How to send data back to previous sceen when using navigation.goBack()? 如何使用 goBack() function 在 React Native 中将 go 返回到上一个屏幕? - How to go back to previous screen in React Native with goBack() function? 反应导航 goBack() 并在上一个屏幕中更新 - React navigation goBack() and update in previous screen React-Native goBack() 不导航到上一个屏幕 - React-Native goBack() not navigating to previous screen React Native,reactnavigation,如何使用 Drawer 返回()到上一个屏幕,而不是第一个屏幕? - React Native, reactnavigation, how do I goBack() to the previous screen, not the first screen, using a Drawer? goBack 如何在排毒测试中筛选 - How goBack screen in test with detox 如何将数据从下一个屏幕发送到上一个屏幕和 append 数据在上一个屏幕中的数组中 state 变量在反应本机 - How to send data from next screen to previous screen and append data in an array in previous screen state variable in react native 导航 goBack() 可以到前一个屏幕的 Flatlist 的确切高度吗? - Navigation goBack() can go to exact height of Flatlist of previous screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM