如何在 React Native Navigation v5 中将数据传递回上一个屏幕?

sch*_*mru 4 javascript typescript react-native react-native-navigation react-navigation

我刚刚更新以响应本机导航版本 5。现在我尝试在 goBack() 调用上将数据发送回上一个屏幕。

我推动下一个视图

const onSelectCountry = item => {
    console.log(item);
};

navigation.navigate('SelectionScreen', {
        onSelect: onSelectCountry});
Run Code Online (Sandbox Code Playgroud)

并在通过调用从 FlatList 选择项目后返回:

function onSelectedItem(item) {
    route.params.onSelect(item);
    navigation.goBack();
}
Run Code Online (Sandbox Code Playgroud)

但是通过使用参数发送函数,我收到警告:在导航状态中发现不可序列化的值...

有人可以告诉我正确的方法吗?

小智 7

这是一个实现

屏幕A

const Screen1 = ({navigation, route}) => {
  const [item, setItem] = useState(null);

  useEffect(() => {
    navigation.addListener('focus', () => {
      console.log(route.params)
    })
  }, [])  

  const onPress = () => {
    navigation.navigate('Screen2', {onReturn: (item) => {
      setItem(item)
    }})
  }
  return (
    // Components
  )
}
Run Code Online (Sandbox Code Playgroud)

屏幕2:

const Screen2 = ({navigation, route}) => {

  useEffect(() => {
    navigation.addListener('focus', () => {
      console.log(route.params)
    })
  }, [])  

  // back Press
  const onPress = () => {
    route.params.onReturn(item);
    navigation.goBack()
  }

  return (
    // Components
  )
}
Run Code Online (Sandbox Code Playgroud)