mobx-react icon indicating copy to clipboard operation
mobx-react copied to clipboard

Why does mobx-react observer break react-router-dom navlink active class?

Open cedcn opened this issue 9 years ago • 7 comments

http://stackoverflow.com/questions/43661560/why-does-mobx-react-observer-break-react-router-dom-navlink-active-class

I have same issues.

cedcn avatar May 05 '17 05:05 cedcn

I am not react-router user, but I think this might relate to the fact that observer also automatically applies pure render mixin - see following. Try to provide own shouldComponentUpdate implementation, it should take precendence over Mobx default behavior. See docs:

observer also prevents re-renderings when the props of the component have only shallowly changed, which makes a lot of sense if the data passed into the component is reactive. This behavior is similar to React PureRender mixin, except that state changes are still always processed. If a component provides its own shouldComponentUpdate, that one takes precedence

urugator avatar May 06 '17 19:05 urugator

you can try use withRouter

xieqingtian avatar May 08 '17 05:05 xieqingtian

Using the withRouter decorator before all mobx decorators fixes the issue for me.

Is there a more convenient way than wrapping each and every component in withRouter?

JoschuaSchneider avatar May 12 '17 05:05 JoschuaSchneider

Just compose your decorators:

function observableWithRouter(component) {
   return withRouter(observable(component))
}

Closing the issue as imho this is primarily an issue with RR, not with MobX (same issue arises when using RR with Redux), and there is a known solution to it. For more background on the issue see: https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076

mweststrate avatar May 13 '17 19:05 mweststrate

Thanks guys.

cedcn avatar May 14 '17 04:05 cedcn

the same problem here, but with @withRoute decorator from 'react-router' package works perfectly. thanks, guys. I spent 3 days :sob:

jricaldi avatar Oct 27 '17 07:10 jricaldi

For future reference, this is extensively described in the react router docs and will be addressed in the router package in the fuure: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md

mweststrate avatar Dec 04 '17 08:12 mweststrate