如何解决如何在React-Native中通过React-Navigation多次传递参数?
function A() {
const [levels,setLevels] = React.useState(null);
// upon the component mount,we call the database.
React.useEffect(() => {
database()
.ref()
.once("value")
.then((snapshot) => {
//get Levels from db
const levels = snapshot.map((child) => child.key);
setLevels(levels);
});
},[]);
return (
<View>
{levels.map((element) => {
return <B level={element} />;
})}
</View>
);
}
function B(props){
const navigation = useNavigation();
return (
<View>
<TestButton
title={props.level}
onPress={() => navigation.navigate('C')}
/>
</View>
);
}
我目前已经将'level'值传递给TestFunc参数,但是现在我希望将相同的参数传递给按下TestButton组件时导航到的屏幕。
https://reactnavigation.org/docs/params/
此文档向我展示了如何使用新初始化的参数来执行此操作,但它似乎不适用于从先前屏幕传递来的参数。
有什么建议吗?
编辑: 我试图使其更加清晰。我想从功能A到功能B到功能C,我的应用程序中的所有不同组件/屏幕。我想获取从函数A获得的“级别”,并将其从B传递给C。目前,我正在正确地检索函数B中的“级别”值,但是似乎无法将其传递给函数C。>
解决方法
function TestFunc (props){
const navigation = useNavigation();
return (
<View>
<TestButton
title={props.level}
onPress={() => navigation.navigate('Category',{...props})}
//{...props} send all props that's exist in props argument
/>
</View>
);
}
您还可以在Category组件上获得所有道具
const { level } = this.props.navigation.state.params;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。