6

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.

Dmitry Sokurenko
  • 6,042
  • 4
  • 32
  • 53

1 Answers1

2

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
}
enjoylife
  • 3,801
  • 1
  • 24
  • 33