简体   繁体   English

反应路由器私有路由实现是否通过了路径道具?

[英]react router private route implementation is path props passed or not?

Hello everyone this is more of a request asking review on my understanding of private route implementation in react.js rather than a question.大家好,这更像是一个要求审查我对 react.js 中私有路由实现的理解的请求,而不是一个问题。 Many implementations of private routes with authentication seem to have this same code posted许多带有身份验证的私有路由的实现似乎都发布了相同的代码

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
    fakeAuth.isAuthenticated === true
      ? <Component {...props} />
      : <Redirect to='/login' />
  )} />
) 

now as far as I know the 3 dots is the spread operator which basically assigns the passed object. Further SO answer also corroborates it What do these three dots in React do?据我所知,这 3 个点是传播运算符,它基本上分配传递的 object。进一步的 SO 答案也证实了 React 中这三个点的作用是什么? especially the answer from Mehdi Raash.特别是 Mehdi Raash 的回答。 Now my understanding is that this basically means that the 'path' prop is also passed in the...rest object just like现在我的理解是,这基本上意味着“路径”道具也在...rest object 中传递,就像

<Route path={rest.path} render={(props) => (
        fakeAuth.isAuthenticated === true
          ? <Component {...props} />
          : <Redirect to='/login' />
      )} />

but recently i have came across this posthttps://ui.dev/react-router-v4-protected-routes-authentication/ and this line just doesn't make sense to me但最近我遇到了这篇文章https://ui.dev/react-router-v4-protected-routes-authentication/这条线对我来说没有意义

A few notes on the above code. With Route, if a path isn’t supplied, then that Route will always match. So in the case above, because we didn’t supply a path prop, the Route will always match which means the render prop will always be called. 

So I just want to know whether this statement is correct and path props isn't being passed in the private route implementation and if so then what is this {...rest} doing in the Route.所以我只想知道这个声明是否正确,路径道具是否没有在私有路由实现中传递,如果是这样,那么这个 {...rest} 在 Route 中做了什么。

Note: No offence for the author behind the article i just want my understanding stand corrected if I am wrong注意:文章背后的作者没有冒犯我只是希望我的理解得到纠正,如果我错了

The spread operator basically puts all remaining properties into a new object called rest in your case.在您的案例中, 传播运算符基本上将所有剩余属性放入一个名为rest的新 object 中。 Let's say that PrivateRoute was initiated like this:假设PrivateRoute是这样启动的:

<PrivateRoute component={Home} path="/protected" secure={false} />

component was desctructured and renamed to Component in your example.在您的示例中, component被分解并重命名为Component This means that rest looks like this:这意味着rest看起来像这样:

rest = {
  path: "/protected",
  secure: false
}

What happens next is that the rest object is spreaded into the Route .接下来发生的是rest object 被传播到Route中。 So this:所以这:

<Route {...rest} render={(props) => ()} />

is the same as this:与此相同:

<Route path={res.path} secure={res.secure} render={(props) => ()} />

or this:或这个:

<Route path="/protected" secure={false} render={(props) => ()} />

By the way, the props in the render() function only contain three props and they come from the Router:顺便说一句, render() function 中的道具只包含三个道具,它们来自路由器:

  • match比赛
  • location地点
  • history历史

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

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