react-router: this.context.router is undefined
I just upgraded to react-router (and react) 0.13, and got rid of all the deprecated State mixins. Using this.context.router doesn’t seem to work. I just see this everywhere:
Uncaught TypeError: Cannot read property 'getParams' of undefined
Is there anything additional I need to do?
Edit by Ryan Florence to get people to the answer right here at the top of the issue 💃
Check out the upgrade guide: https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md#012x---013x
Sorry it wasn’t done w/ the release, it’s been an abnormally busy month for us since we quit our jobs and started a new business 😛
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 94 (27 by maintainers)
Your class also needs
Using version
0.13.1of react and react-router the following seems to work for me:react-router v2.0.0, you can try with:
If contextTypes is not defined, then this.context will be an empty object.
In general, I don’t like this switch to relying on
contexteverywhere. I’d rather have a clear, well-defined API that I can use rather than rely on invisible magic that’s impossible to debug when it breaks. There’s no stacktrace, no actual error anywhere, and no trail leading to anywhere inside of react-router. It’s just… broken.I was with react-router 0.13.x for now, so I just cut the BS with window.location.hash = ‘/newlocation’
Is the contextTypes changed to Object recently ! I was trying with
Though working, threw warning!
Changed to
And everything it fine now !
I think the answer to the original question posted by @tjwebb is that the property “contextTypes” cannot be attached in the class declaration directly when using ES6. ES6 allows methods on the class declaration but not regular properties. To add the contextTypes property you would have to do this:
I was using this solution by @Flourad
And it worked fine. But then I created a new project and I got this weird error.
Now I’m using this:
Which works and makes more sense since router is an object after all, and not a function.
Please use Stack Overflow or Reactiflux for questions – not the issue tracker.
Yes, so this works:
example: http://learnreact.robbestad.com/#/articles/article/1 (check console.log)
@tjwebb My example above is using ES6, in which you cannot use mixins. You have to add the part
after every class declaration in which you want to use
this.context.router.I suggest removing the deprecation warning then, otherwise it might be confusing for users (like me 😉).
Sounds good to me, although a bit cluttered, right now
propsreally are the only way to safely pass stuff down the tree.