'TypeScript error after upgrading version 4 useParams () from react-router-dom Property 'sumParams' does not exist on type '{}'
I get a typeScript error after upgrading to version 4 Used in useParams () from react-router-dom
"typescript": "^4.0.2"
import { useParams } from 'react-router-dom';
const { sumParams } = useParams();
Property 'sumParams' does not exist on type '{}'.
The project worked great and only after the upgrade does it throw an error
Solution 1:[1]
Another option is:
const { sumParams } = useParams() as {
sumParams: string;
}
Solution 2:[2]
To make it function as before, just add ":any"
const { sumParams } : any = useParams();
Solution 3:[3]
type ParamTypes {
sumParams: string;
}
const { sumParams } = useParams<ParamTypes>()
this would be clean approach to take
Solution 4:[4]
For newer versions of the router, generic is changed from object to union of strings.
const { param1, param2 } = useParams<'param1' | 'param2'>();
Current useParams type in the react-router types looks like this:
export declare function useParams<Key extends string = string>(): Readonly<Params<Key>>;
Solution 5:[5]
You can also access the params from useRouteMatch if you already have it imported in your file
const curRoute = useRouteMatch();
// @ts-ignore
let sumParams = curRoute.params.sumParams
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | keemor |
| Solution 2 | Yoel |
| Solution 3 | Shailesh Parmar |
| Solution 4 | Mario Petrovic |
| Solution 5 | Caleb C. Adainoo |
