简体   繁体   中英

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

How to send data in previous screen with help of 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>
  );
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM