简体   繁体   English

为什么我需要为路由参数使用大括号?

[英]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.

相关问题 AngularJS取消路线-为什么需要使用$ rootScope? - AngularJS Route Cancelling — why do I need to use $rootScope? 为什么我需要为动作创建者添加花括号? - Why do I need to add curly braces to the action creator? 在这种情况下,我应该使用大括号{}或方括号[]吗? - Should I use curly brackets {} or square brackets [] in this case? 我需要通过ajax发送get请求,如何使用接受参数的路由 - I need to send get request via ajax, how do I use route that accepts a parameter 使用花括号时的角度 - angular when to use curly brackets 为什么我们必须在路由参数“/:userQuery”中使用冒号 (:) - why do we have to use colon (:) in the route parameter “/:userQuery” 为什么嵌套JSX需要花括号,即使没有它也可以使用 - Why do I need curly braces for nested JSX, even if it is working without it 为什么ng-click无法发送带有大括号{{}}的函数参数? - Why ng-click is unable to send function parameter which is embraced with curly {{ }} brackets? 访问匿名自执行函数的参数时,为什么需要使用“ this”关键字? - Why do I need to use the “this” keyword when accessing a parameter for an anonymous self-executing function? 为什么我需要在Ember.js中定义索引路由? - Why do I need to define an index route in Ember.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM