Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access the current route component with React Router

For example I have routes like:

<Route path="about" component={AboutPage} />
<Route path="status" component={StatusPage} />

Lets assume that the current route is '#/about'. I need something like routerInstance.currentComponent to return AboutPage component instance.

Does React Router has a method to do it?

ps. I understand that accessing component instances from the outside is not the React Way to do things, but nevertheless I need it.

like image 761
Dmitry Sokurenko Avatar asked Dec 03 '15 22:12

Dmitry Sokurenko


1 Answers

not sure if react-router already exposes it, theres an old GitHub discussing undocumented api's, here but you could set it yourself per Route.

//AboutPage
componentDidMount() {
  global.currentComponent  = this
}

//StatusPage
componentDidMount() {
  global.currentComponent  = this
}
like image 126
enjoylife Avatar answered Oct 02 '22 21:10

enjoylife