[英]Why do I need to use curly brackets for the route parameter?
I am using react navigation v5 and I am pushing a parameter from one screen to another.我正在使用反应导航 v5,并且正在将参数从一个屏幕推送到另一个屏幕。 I made it work, but I cannot understand, why I need to use the curly brackets there.
我让它工作了,但我不明白,为什么我需要在那里使用大括号。 And where would I add the props parameter?
我会在哪里添加 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>
)
}
As I said it is working as this, but usually I have something like正如我所说,它是这样工作的,但通常我有类似的东西
const userProfile = (props) =>
How would I add props to the 2 screen?如何将道具添加到 2 屏幕? And why do I need to use the curly brackets and how do I know if I can add it as a parameter like props or if I need to use the brackets.
以及为什么我需要使用大括号,我怎么知道我是否可以将它添加为像 props 这样的参数,或者我是否需要使用大括号。
Thanks!谢谢!
Update: I found out, that it is also working with props.route.params.id.更新:我发现它也适用于 props.route.params.id。 I am very confused now.
我现在很困惑。
This is called Destructuring in javascript...这在javascript中称为解构...
here you're extracting route
out of userProfile
props
在这里,您从
userProfile
props
中提取route
const userProfile = ({ route }) => {
which is equivalent to这相当于
const userProfile = (props) => {
const route = props.route;
};
== ==
const userProfile = (props) => {
const { route } = props;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.