I have an application, that uses the class component syntax to create all components so far. With these package versions:
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
I am trying to get my route parameters to my components. So I started with:
<Routes>
<Route path="/Login" element={<Login authSuccessCallback={() => <Navigate to="/" />} />}></Route>
<Route path="/EmailVerify/:id" element={<VerifyEmail />}></Route>
<Route path="*" element={UserManager.IsLoggedIn() ? <></> : <Navigate to="/Login" />}></Route>
</Routes>
I alredy tried to:
- extend my props with:
RouteComponentProps<VerifyEmailRouteParams>
private id = useParams<VerifyEmailRouteParams>();
private params = useParams<{id:string}>();
- just read from the params property in my props
this.props.params.id
(don't know why I'd expected this to work) <Route path="/Contacts/VerifyEmailAddress/:id" render={(params) => <VerifyEmail {...params}></VerifyEmail>}>
I already found a lot on the web like:
- https://www.youtube.com/watch?v=J6jzDfHoj-Y
- https://www.pluralsight.com/guides/react-router-typescript
- React this.props.params undefined
I checked my props object with the debugger: I also tried to use the render prop in the route, which is also undefined.
Is it the wrong approach to use class components, typescript or react-router together? Do I miss a library or type imports? (expected react comes with all needed types) Maybe a version mismatch? The router without parameters is working like a charme, but as soon I try to read parameters it will not compile anymore or have runtime issues.