简体   繁体   English

ReactJS React-Router将道具传递给孩子

[英]ReactJS React-Router Passing Props To Child

I have this Private Router, but am struggling to pass in a prop from the state of my app component. 我有这个专用路由器,但是正在努力从我的应用程序组件的状态传递一个prop。 Could someone please explain how to pass in the user object? 有人可以解释如何传递用户对象吗?

<Route
  {...rest}
  render={(props) => authed === true
    ? <Component {...props} />
    : <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>


export default class App extends Component {
state = {
authed: false,
loading: true,
user: false
}

If this Route is inside the App render method, then you can make it like that: 如果此Route在App render方法内,则可以这样:

<Route
  {...rest}
  render={(props) => this.state.authed === true
    ? <Component {...props} user={this.state.user} />
    : <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>

Your Component will get the react-router props (history, match, location) and the user that you maintain in your App component state. 您的组件将获取react-router道具(历史记录,匹配​​项,位置)以及您在App组件状态下维护的用户。

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

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