简体   繁体   English

React Router v4 HOC with render props

[英]React Router v4 HOC with render props

I want to pass a props and declare a HOC in my route for authentification 我想通过道具并在我的路线中宣布一个HOC进行身份验证

Currently, I use 目前,我用

<Route exact path="/add" component={requireAuth(Add)} />

and it is working but without props. 它工作但没有道具。

I believe to pass a props you need to use the render syntax like this 我相信传递道具你需要使用像这样的渲染语法

<Route exact path="/add" render={props => <Add {...props} type="MyProp" />}/>

But is there any way to pass a HOC here? 但有没有办法在这里通过HOC?

Thanks! 谢谢!

Yes, you can . 是的你可以 。 assign a capitalized variable to requireAuth(Add) . requireAuth(Add)分配一个大写变量。

const AuthAdd = requireAuth(Add);
<Route
  exact
  path="/add"
  render={props => <AuthAdd {...props} type="MyProp" />}
/>;

Use withRouter . withRouter使用。

<Route exact path="/add" component={withRouter(Add)} />

Inside your Add class you will then be able to access the router props. Add类中,您将能够访问路由器道具。

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

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