alternative to useParams() in class components in React18, Route 6

26 views Asked by At

I have a React frontend and class components inside it. Moving to React 18 and Route 6, some important changes arose. One difficult problem is getting route params. They proudly introduced hooks but all the examples I found are with function components and using useParams. Unfortunately, useParams doesn't work with class components. The only solution I found till now is to use same function components just as wrappers around class components but this is cumbersome, especially when Redux and Thunk are involved. For example, it took me a lot of time only to find out that useDispatch has to be cast to ThunkDispatch and I am sure the problems won't stop here. I also wouldn't like to change all my class components to function components. As a matter fact I prefer the former.

0

There are 0 answers