[英]Why do I need to use curly brackets for the route parameter?
我正在使用反应导航 v5,并且正在将参数从一个屏幕推送到另一个屏幕。 我让它工作了,但我不明白,为什么我需要在那里使用大括号。 我会在哪里添加 props 参数?
1 screen
<TouchableOpacity onPress={() => { nav.navigate('userProfile', { userId: 1 }) }} >
2 screen
const userProfile = ({ route }) => {
const { id } = route.params;
return (
<View>
<Text>{id}</Text>
</View>
)
}
正如我所说,它是这样工作的,但通常我有类似的东西
const userProfile = (props) =>
如何将道具添加到 2 屏幕? 以及为什么我需要使用大括号,我怎么知道我是否可以将它添加为像 props 这样的参数,或者我是否需要使用大括号。
谢谢!
更新:我发现它也适用于 props.route.params.id。 我现在很困惑。
这在javascript中称为解构...
在这里,您从userProfile
props
中提取route
const userProfile = ({ route }) => {
这相当于
const userProfile = (props) => {
const route = props.route;
};
==
const userProfile = (props) => {
const { route } = props;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.