简体   繁体   English

没有TypeScript的情况下,这种类型注释如何在React代码中工作?

[英]How is this type annotation working in React code without TypeScript?

I was looking at this code example on the ReactRouter page, and this piece is interesting: 我在ReactRouter页面上看了这个代码示例 ,这很有趣:

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={props =>
      fakeAuth.isAuthenticated ? (
        <Component {...props} />
      ) : (
        <Redirect
          to={{
            pathname: "/login",
            state: { from: props.location }
          }}
        />
      )
    }
  />
);

The component: Component part looks like a type annotation. component: Component部分看起来像类型注释。 Webstorm didn't complain when I put this example into an empty file, but I didn't see them using Flow or TypeScript or anything in the imports. 当我将示例放到一个空文件中时,Webstorm并没有抱怨,但是我没有看到它们使用Flow或TypeScript或导入中的任何内容。

Does JavaScript have Type Annotation already? JavaScript已经有类型注释了吗? I didn't see anything on MDN about this when I searched, and also React doesn't automatically provide type annotation either from what I learned... 当我搜索时,我在MDN上没有看到任何关于此的信息,而且React也不自动从我学到的内容中提供类型注释。

It's not a type annotation. 这不是类型注释。 It's a ES6 feature called destructuring rest parameters : 这是一个称为ES6的功能,可分解剩余参数

First consider this example: (destructuring assignment) 首先考虑以下示例:(销毁分配)

const test = myObject.prop
// prop can be assigned in a variable
const { prop } = myObject
// but we want prop as a test variable
// and can be written as
const { prop: test } = myObject
// which is similar to first line
console.log(test)

This is how we can pass in the parameters: 这是我们可以传递参数的方式:

({ component, ...rest })

// We're giving component name as Component

({ component: Component, ...rest })

So, that you can use <Component /> as you know lowercase component <component /> is invalid in react. 因此,您可以使用<Component />因为您知道小写组件<component />在react中是无效的。


Furthermore, I would suggest you to deep dive into the following blogs: 此外,我建议您深入了解以下博客:

Hackernoon: Understanding the destructuring assignment Hackernoon:了解解构任务

JavaScript Info: Destructuring assignment JavaScript信息:解构分配

Codeburst: Destructuring assignment the complete guide Codeburst:销毁作业完整指南

Dev.to: Destructuring assignment - array Dev.to:解构分配-数组

Dev.to: Destructuring assignment - object Dev.to:解构分配-对象

What you are seeing is not a type annotation, but a property in an object pattern. 您看到的不是类型注释,而是对象模式中的属性。 Let's simplify your example to help see what is going on. 让我们简化您的示例以帮助查看发生了什么。

Here's a function that is easy to understand: 这是一个易于理解的函数:

f = (h) => [h.x, h.y]

The function f takes in an object h and returns an array with hx and hy . 函数f接收对象h并返回包含hxhy的数组。 Now in modern JavaScript one does not have to pass in an object and break it all apart in the function body. 现在,在现代JavaScript中,不必传递对象并将其在函数主体中分开。 Instead, we make the parameter section of the function be a pattern so that we don't need to bother with that h variable at all. 取而代之的是,我们将函数的参数部分设置为模式 ,这样就完全不必理会h变量了。 So we could rewrite it like this (in a way that parallels your example): 因此,我们可以像这样重写它(与您的示例类似):

f = ({x: xValue, y: yValue}) => [xValue, yValue]

So, exactly like before, you can pass to f any object that has x and y properties and it will return an array of the values at those properties. 因此,就像以前一样,您可以将具有x和y属性的任何对象传递给f,它将返回这些属性处的值的数组。

Here it is in action: 它在起作用:

> f = (h) => [h.x, h.y]
[Function: f]
> f({x:1, y:2, z:3})
[ 1, 2 ]
> f = ({x: xValue, y: yValue}) => [xValue, yValue]
[Function: f]
> f({x:1, y:2, z:3})
[ 1, 2 ]

As an aside, normally one would skip the xValue and yValue part and have: xValue yValue ,通常会跳过xValueyValue部分并具有:

f = ({x: x, y: y}) => [x, y]

which due to shorthand property notation is just: 由于简写的属性表示法,它只是:

f = ({x, y}) => [x, y]

but as @BhojendraRauniyar writes in another answer, there are capitalization conventions in the framework this code is being used with. 但是正如@BhojendraRauniyar在另一个答案中所写的那样,使用此代码的框架中存在大写约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM