[英]What's ES6 magic is happening in this React Component?
I'm following this tutorial https://serverless-stack.com/ and am on https://serverless-stack.com/chapters/create-a-route-that-redirects.html 我正在关注本教程https://serverless-stack.com/ ,我在https://serverless-stack.com/chapters/create-a-route-that-redirects.html
This introduces an AuthenticatedRoute
which checks the value of a prop called isAuthenticated
to decide weather or not to render the component or redirect the user to login
这引入了
AuthenticatedRoute
,它检查名为isAuthenticated
的prop的值,以决定是否天气呈现组件或重定向用户login
import React from "react";
import { Route, Redirect } from "react-router-dom";
export default ({ component: C, props: cProps, ...rest }) =>
<Route
{...rest}
render={props =>
cProps.isAuthenticated
? <C {...props} {...cProps} />
: <Redirect
to={`/login?redirect=${props.location.pathname}${props.location
.search}`}
/>}
/>;
I get what it achieves, but I'm unsure as to how it's doing it. 我得到了它所取得的成就,但我不确定它是如何做到的。
Can someone explain to me what's going on with the following bits please? 有人可以向我解释下面有什么问题吗?
component: C
...rest
<C {...props} {...cProps} />
The AuthenticatedRoute
is a Functional (stateless) component - a function. AuthenticatedRoute
是一个功能 (无状态)组件 - 一个功能。
The component is invoked with the props as the 1st argument, and this line ({ component: C, props: cProps, ...rest })
destructures the props, and assign some of them to variables. 使用props作为第一个参数调用该组件,并且该行
({ component: C, props: cProps, ...rest })
解构道具,并将其中一些分配给变量。 The component: C
extract the component
property from the props object, and assigns it to variable C
. component: C
从props对象中提取component
属性,并将其分配给变量C
The ...rest
in ({ component: C, props: cProps, ...rest })
is part of the ECMAScript's Object Rest/Spread Properties proposal , and you need babel's Object rest spread transform for it to work in current browsers. ...rest
({ component: C, props: cProps, ...rest })
是ECMAScript的Object Rest / Spread Properties提议的一部分 ,你需要babel的Object rest spread变换才能在当前的浏览器中工作。 It creates a new object from all the object properties that weren't assigned to variables (the rest). 它从未分配给变量的所有对象属性(其余)创建一个新对象。
The <C {...props} {...cProps} />
is react's JSX spread attributes , and it converts all the object ( props
and cProps
) properties to component attributes (like writing key=value). <C {...props} {...cProps} />
是react的JSX spread属性 ,它将所有对象( props
和cProps
)属性转换为组件属性(如写入key = value)。 Props in cProps
will override the properties of props
because they appear after them. cProps
的props
将覆盖props
的属性,因为它们出现在props
之后。
component: C - In ES6 you can have default initialisation in case the parameter is not passed. component:C - 在ES6中,如果未传递参数,则可以进行默认初始化。 Here component will be defaulted to C.
这里的组件将默认为C.
...rest- With ES6 you can spread the element of a data structure. ...休息 - 使用ES6,您可以传播数据结构的元素。 Here rest may have the route path for the render function.
这里休息可以有渲染功能的路径路径。
- Each element of cprops and props is passed as attribute to C component. - cprops和props的每个元素都作为属性传递给C组件。 Props in cProps will override the properties of props as they appear after them.
cProps中的道具将覆盖道具后面出现的道具属性。
In this case Authenticated Route
is that is called High Order Component. 在这种情况下,
Authenticated Route
被称为High Order Component。 Authenticated Route
wrap your Route
component and conditionally return component: C
if user is authenticated or Redirect
component. Authenticated Route
包裹Route
组件并有条件地返回component: C
如果用户已通过身份验证,则为component: C
或Redirect
组件。
component
its a simple props of Authenticated Route
but its renamed to C
in cause that react component must be capitalized. component
是Authenticated Route
一个简单的道具,但它重命名为C
,因为反应组件必须大写。 props: cProps
its a props you want to receive to the component props: cProps
它想要接收到组件的道具 props
its a Route
render props that needs to be applied to the component, which the render method should return props
其一个Route
渲染需要被应用到组件道具,其中所述渲染方法应该返回 ...rest
its an object which contain any other props, basically refer to the Route
...rest
它包含任何其他道具的物体,基本上是指Route
Then you can simply wrap use <AuthenticatedRoute />
and pass props you need according to the information written above 然后你可以简单地使用
<AuthenticatedRoute />
包装并根据上面写的信息传递你需要的道具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.