How to use useRouter() from next.js in a class component?
In general, it’s possible to create a wrapper functional component to pass custom hooks into class components via props (but not useful in this case):
1 2 3 4 5 6 7 8 9 10 11 12 |
const MyClassWithRouter = (props) => { const router = useRouter() return <MyClass {...props} router={router} /> } class MyClass... constructor(props) { this.state = { loc: props.router.query.loc, loaded: false }; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.