I've got a page on my Next.js app that does the following:
- It's a search page
- The pathname is like:
/search?q=search+slug - It loads data on the client
- It needs to read the
router.queryto get therouter.query.qvalue
PS: I'm using Redux
const dispatch = useDispatch();
const router = useRouter();
const query = router.query as { q: string };
const queryString = query.q;
console.log("SEARCH CONTAINER");
console.log(`queryString: ${queryString}`);
useEffect(() => {
dispatch(THUNK.LOAD(queryString));
return () => { dispatch(ACTION.RESET_STATE()); };
},[dispatch,queryString]);
See the useEffect. In theory is should run only once for every queryString (which is actually req.query.q).
But I was getting duplicated THUNK.LOAD actions. That's why I've added the console.log() there.
And this is what it's logging out:
And then:
And this is why I'm getting duplicated dispatches. Of course I can check for if (queryString) before dispatching, or maybe I can get it from window.location.search. But I am surprised the router.query.q comes as undefined on the first place. How is that even possible? Why would the req.query object be populated asynchronously? What is the explanation for this?

