score:0

Accepted answer

If i understand correctly what you are trying to achieve is to get the name of the Component / Page you are in dynamically.
If this is the case, you can add a prop called name to the route and grab it inside the component.
For example:

 <Route path="/" name="App" component={App}>
  <IndexRoute name="Home Page" component={HomePage} />
  <Route path="/home" name="Home Page" component={HomePage} />
  <Route path="/about" name="About Page" component={AboutPage} />
  <Route path="*" component={NotFoundPage} />
</Route>

and inside the component this is how to grab the name:

render() {
// ...
    const currentRouteName = this.props.routes[this.props.routes.length - 1].name;
// ... rest of code

score:0

You have to update your all child component to have additional property "title". For ex update Home component as below

Home.title = "HomePage";

export default Home

Now update your routes to pass this info as a prop

<Route component={Layout} onEnter={checkIndexAuthorization(store)}>
     <IndexRoute component={Home} />
     <Route path="/home" component={Home} title={Home.title} />
</Route>

Now in Layout component you can access this from children props like below

<header>
    {props.children.route.component.title}
</header>

score:1

I was just looking at this too - in my application I have private routes which are only available after Authentication, which I only remembered after about 5 minutes of looking at this!

If you are creating private routes in your application, do not forget to pass props to the newly created route component e.g.:

<PrivateRoute path="/path/to/route" name={'Route name'} component={RouteComponent} layout={SimpleLayout} />

And the definition for PrivateRoute:

const PrivateRoute = ({ layout: Layout, component: Component, ...rest }) => (
    <Route {...rest} render={props => {
        const newComponent = Layout ? 
             <Layout><Component {...rest} {...props} /></Layout> 
                 : 
             <Component {...props} />;

        return Auth.isUserAuthenticated() ? (
            newComponent
        ) : (
            <Redirect to={{pathname: '/login', state: { from: props.location }}} />
        )
    }}/>
);

The important part is remembering to spread to current props onto the new Component {...rest}.

Then in the actual Layout file (eg. SimpleLayout above) you can access the props via:

this.props.children.props.name

I'm sure there are still better ways of doing this, but from my experience thus far, this worked ok for me!


Related Query

More Query from same tag