Beginner here, but finding this quite tricky. So some help would be appreciated!
I want to have users filter through some options. Those filters should be reflected in the URL. e.g. : http://localhost:3000/items?counter=1
Now when a user visits http://localhost:3000/items?counter=2
I want that to be reflected in state & put it in state. If the same user then changes the state somehow, I want that to be reflected in the url. I do know how to do both things.
But I feel I am running into an infinite loop here:
useEffect(() => {
router.push(`/items?counter=${counter}`, undefined, { shallow: true })
}, [counter])
useEffect(() => {
setCounter(parseInt(router.query.counter))
}, [router.query.counter])
How would I best derive my state from my query params but also always shallow-update the query params every time state changes?