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.

Router Context is Swallowed

See original GitHub issue

I am here to resurrect #19 and #16, for which I apologize.

I believe the current state of things is that mobx (and mobx-react-router) don’t really work out of the box with react-router v4.

Cause: observer components use a customized shouldComponentUpdate that pays attention to the mobx context but not the context that react-router needs.

Here’s a repro:

Current workaround: Users of this lib should wrap withRouter around every observer in their app.

Solutions: Though this isn’t mobx-react-router’s fault (ReactTraining/react-router#4781, etc, etc) it does appear that this library owns the space at the intersection of mobx and react-router and is therefore in the best position to bundle a fix or workaround.

Short-term solution: This should be called out prominently in the readme. It’s relevant to ~100% of people who use this lib (I think?)

Longer-term solutions: A fix or workaround should be integrated into this lib if possible. I think it is possible! Anything is possible, right? Here are some potential approaches:

  1. Whatever react-router-redux does. Assuming it has this problem solved, which I haven’t verified.
  2. As part of syncHistoryWithStore, somehow turn the history attributes that react-router uses into observables. If that’s a thing.
  3. Add a function/decorator that takes the context react-router needs out of the routing store and puts it into the routing context of the child component. You’d still need to apply this decorator to anything that uses a Route or NavLink, but at least you don’t have to wrap every single observer in withRouter.
  4. Provide a router-friendly alternative to observer as part of this lib. This at least provides a solution out of the box and gets people like me to stop writing our own dubious one-offs.

Happy to submit a PR for the README or to look into options 3/4 if you agree with the above.

Thanks! Daniel

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
clementdevosmccommented, Oct 17, 2017

Does someone have a complete working example of this workaround? Been playing with this for the past hours and still did not manage to make the whole thing work together…

1reaction
alisd23commented, Oct 19, 2017

@clementdevosmc Notice the @withRouter wrapped wrapped round certain components. Worth reading this to understand the problem: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Complete Guide to React Router: Everything You Need to ...
To make that pill a little easier to swallow, this post is a ... React Context helps make history available wherever React Router...
Read more >
Routers - Spring
Routers and the Spring Expression Language (SpEL) ... a single channel, any caused exception is swallowed, which usually makes little sense.
Read more >
React Router Integration - Sentry Documentation
React Router v4/v5​​ The React router instrumentation uses the React router library to create pageload/navigation transactions and paramaterize transaction names ...
Read more >
IP Router Alert Considerations and Usage RFC 6398
This document discusses security aspects and usage guidelines around the use of the current IP Router Alert Option, thereby updating RFC 2113 and...
Read more >
Higher-Order Components in React Hooks era - Developer way
Redux connect or react-router's withRouter functions are ... 90% of shared logic concerns and 100% of use-cases for accessing context, ...
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