Ayrton Dumas
Ayrton Dumas

Reputation: 85

React router - personal props undefined

Hello people

I'am trying to pass a props through a react route ( user={user} ):

<Router history={browserHistory}>
        <Route path="/" component={MainLayout} user={user}> 
          <IndexRoute component={DocumentsPage}></IndexRoute>
          <Route path="about" component={AboutPage}></Route>
          <Route path=":id/preview" component={PreviewPage}/>
          <Route path="upload" component={UploadPage}></Route>
        </Route>
      </Router>

but in my layout, the this.props.user is undefined.

So, here are my two questions:

  1. Is it possible to pass props through a Route ?
  2. Can i set session-like variable in React ?

Thank you <3

Upvotes: 0

Views: 144

Answers (1)

abhirathore2006
abhirathore2006

Reputation: 3745

you can pass the props in route however accessing the prop will be a tedious task in this case because this.props.routes contains array of loaded routes and in that array one of the route contains the prop you passed, however if you assign a name prop to all routes then you can loop them and know exactly which index of routes array contain the prop you want to access but the better choice will be to use redux(my choice) or flux to access and pass props to components

Upvotes: 2

Related Questions