question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple onEnter/onLeave Hooks on <Route>

See original GitHub issue

Summary

I think it might be very helpful to allow users to pass either a single function (as they currently can) or an array of functions (proposed new feature) to the onEnter and onLeave props of Route components. I think this could allow developers to create much more powerful and flexible actions on route transitions.

Example Use Case

This is something that has come up in the app I’m working on now. As the router is currently configured, every leaf route has a single onEnter hook that dispatches actions to fetch all data dependencies from the server and then records page views for Google Analytics. This works pretty well right now because every leaf route does both of those things, so we only need one hook. However, we’re starting to run into cases where some routes have more highly customized needs, particularly with analytics.

I’ll explain one such case in more detail. Certain routes may have referrer tokens in the query string that we would like to capture and send to Google Analytics. Unfortunately, we can’t just rely on the normal referral URL that GA automatically sends on initialization, since these referral links could be placed on arbitrary domains. For those routes (and only those routes), I would like to send an event to Google Analytics that contains the referral token. Unfortunately, adding this new onEnter functionality to some routes and not others requires that we write a new hook for those particular routes. I don’t see that as a scalable solution. Plus, providing a good name for that function is a nightmare - which to me is a major code smell.

Our workaround right now is to put the custom, route-specific actions in the lifecycle hooks of the route handler components themselves. It’s functional, and it doesn’t strike me as hack-y. But it’s not the ideal solution to my mind. I think enabling multiple onEnter hooks would provide a simpler, more expressive, and declarative way for us to create these more flexible transition actions.

Code Sample

/* Suggested API for passing multiple onEnter hooks */

<Router history={History}>
  <Route path="/" component={App}>
    <Route path="recommendations" component={Recommendations} onEnter={ [fetchDataForComponents, recordPageView, recordReferralToken] } />
    <Route path="profile" component={Profile}>
      <IndexRoute component={requireAuth(ProfileMain)} onEnter={ [fetchDataForComponents, recordPageView] } />
      <Route path="edit" component={requireAuth(ProfileEdit)} onEnter={ [fetchDataForComponents, recordPageView] } />
    </Route>
  </Route>
</Router>

In Closing

It’s possible that I’m missing something that makes this feature either unnecessary or very difficult to implement with the current API. Perhaps there’s a cleaner solution to the problem I described above. If that’s the case, we can close this issue. Otherwise, I’d be happy to take a run at writing the necessary code.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

13reactions
baronswindlecommented, Apr 29, 2016

@chazsolo I wrote a couple of utility functions that you might find useful for composing your hooks. I just put them up on GH here.

0reactions
chazsolocommented, Apr 29, 2016

@bjlewis88 This is very useful - thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple onEnter/onLeave Hooks on <Route> #3103 - GitHub
As the router is currently configured, every leaf route has a single onEnter hook that dispatches actions to fetch all data dependencies from ......
Read more >
Using Hooks with React Router - LogRocket Blog
In this tutorial, you can learn how to use Hooks with React Router and minimize ... For this demo, you'll only have two...
Read more >
How to use react context hook with multiple routes?
I'm working on a Chat Application and i wanted to store the information of the logged user so I used useContext hook for...
Read more >
How To Handle Routing in React Apps with React Router
You will need to do two things. First, get the current path with the useRouteMatch Hook. Next, render the new <Switch> and <Route>...
Read more >
A Complete Beginner's Guide to React Router (Including ...
We can also extend it to build multi-page applications with the help of React ... Redirecting to a 404 page; Guarding routes; Router...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found