简体   繁体   中英

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. 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
  {...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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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