
[英]React-Native — How to pass parameters using react-navigation?
[英]How to pass a paramater multiple times through React-Navigation in React-Native?
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/
该文档向我展示了如何使用新初始化的参数执行此操作,但它似乎不适用于从先前屏幕传递的参数。
有什么建议么?
编辑:我试图让它更清楚一点。 我想要 go 从 function A 到 B 到 C,我的应用程序中的所有不同组件/屏幕。 我想获取从 function A 获得的“级别”并将其从 B 传递给 C。目前我正在正确检索 function B 中的“级别”值,但是我似乎无法将其传递给 function 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.