New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using react-router in react ES6 component with Typescript #5406
Comments
After 3hrs, solved it with ES7 syntax:
|
Ok, still not solved, as now Typescript is complaining about this syntax (the new way react-router is used in ES6 react classes). I'm getting the following TS error now, but this is the only way to use react-router in react ES6 classes:
Narrowed down the culprit to the React TS def. file at:
Commenting out |
Ok, just figured out that TS wanted contextTypes typed, here is the final version for anyone else that may run into this. Not an issue with the react, or react rout-er type definitions:
Hopes that helps any other poor souls trying to use react-router with react ES6 classes in typescript. |
Thanks to you and to Marius Rumpf for making me realize that said contextTypes must be a static property, awesome, don't know what I would do without all of you people who document your findings! |
Great! thanks Mauricio, is this documented somewhere? If not, it should!! |
Thanks, this helped me as well! |
This was very helpful for me too thanks! :) A couple of other notes in case it's helpful to others stumbling here to do programmatic react-router redirection as I was. afaict react-router no longer injects 'router' I instead used history, and also for whatever reason needed to cast the context as 'any' since context.history was flagged as not existing
|
@danielbanfield it was flagged as not existing because the type of context is Ideally it would be nice to change the definition of
It's easy then to pass a custom context type. |
@ppoliani love your idea! |
Basically, I can not find a way to make Typescript understand react-router's new way of using the context in react ES6 components. Mainly, using code like this to get the react-router to function in react ES6 classes:
Gives an error like this:
Property 'contextTypes' does not exist on type 'typeof LoginPage'.
Every React component using ES6 and react-router will need to do this since react's new ES6 features does not support mixins. I am linking the discussion of this issue from the react-router repo (remix-run/react-router#975). Has anyone seen this before? How are people using Typescript, React ES6 components, and react-router?
Is this an issue with the react-router type definitions? react type definitions? none of the above? I'm not sure.
The text was updated successfully, but these errors were encountered: