TypeError: Cannot read properties of null (reading 'useContext')

Viewed 76

In nextjs, I am trying to use useRouter in a component, but it is being shown that useRouter is returing null and so i am not able to get the query params.

// this is how i tried fixing it. But showed up the same error
const { query } = useRouter() || { query: { text: '' } };

error

1 Answers

If what you're doing is trying to get the query with useRouter, you can do something like this,

const { query } = useRouter();

useEffect(() => {
 console.log(query);
 // or set state in here
}, [query]);

Related