javascript - 如何在反应原生导航 v5 中将数据传递回上一个屏幕?
问题描述
我刚刚更新以响应本机导航版本 5。现在我正在尝试在 goBack() 调用上将数据发送回上一个屏幕。
我推下一个视图
const onSelectCountry = item => {
console.log(item);
};
navigation.navigate('SelectionScreen', {
onSelect: onSelectCountry});
并在通过调用从 FlatList 中选择项目后返回:
function onSelectedItem(item) {
route.params.onSelect(item);
navigation.goBack();
}
但是通过使用参数发送函数,我得到一个警告:在导航状态中发现了不可序列化的值......
有人可以告诉我正确的方法吗?
解决方案
这是一个实现
屏幕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
)
}
屏幕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
)
}
推荐阅读
- php - 登录后 Laravel 两个因素不起作用
- javascript - 笑话:比较 DOM 元素和 JavaScript 对象
- python - 如何为这些列的最小值和最大值之间的两个随机数更改两列中的空值?
- html - 根据动态变量更改 dir 属性
- javascript - 在 javascript 类构造函数中声明变量与在所述构造函数中调用的方法是否存在有意义的性能差异?
- angular - 在双重条件下删除 ngIf 项
- matlab - MATLAB 中的 Copy-on-Write 和 varargin
- typescript - 当我不进行代码拆分时,为什么 Rollup 会抱怨代码拆分?
- java - 在 Java 中为现有 API 生成 RAML 文件
- airflow - 如何使用气流的 SSH 操作符运行带有参数的 shell 脚本