Next.js Discord

Discord Forum

Is useRouter the right tool for changing current page query params?

Answered
Britannia Petite posted this in #help-forum
Open in Discord
Britannia PetiteOP
[app router] I have filters on my page which state is stored in query params. When filter is changed i use router to navigate to current page with new query params. Is that correct way of handling this? Also is there any boolean available that indicates loading? I don't want to replace entire page content with loader during query param change, just add tiny spinner in the corner.
Answered by Britannia Petite
Thanks, it wasn't it (router.events is not available) but this gave me an idea. It's as simple as that

  const [isLoading, setIsLoading] = useState(false)

  useEffect(() => {
    setIsLoading(false)
  }, [serverData])


and after updating query params:
setIsLoading(true)
View full answer

27 Replies

Have you tried nuqs? I’ve heard that’s a great library to sync url state, gives you tools for many use cases
Britannia PetiteOP
Just tried nuqs but it doesn't seem to be working with nextjs app router data fetching
@Morelet’s Crocodile Is it possible for the entire content's number to remain unique when the filter is changed?
Britannia PetiteOP
Yes I have list and it's size will change after changing filter
Morelet’s Crocodile
Then how can you navigate to current page?
For example if filter is student, total number is 100, then when filter is teacher , total number is 30.
In that case I think it's impossible to navigate to the current page,
Britannia PetiteOP
Not sure what. you mean. What I have works just fine but I need an loading indicator
Morelet’s Crocodile
For loading indicator what about using isReady variable?
@Britannia Petite Not sure what. you mean. What I have works just fine but I need an loading indicator
Morelet’s Crocodile
const router = useRouter();
 useEffect(() => {
    if (router.isReady) {
      setLoading(false); // Set loading to false when the route is ready
    }
  }, [router.isReady]);
Britannia PetiteOP
I think it's from pages router, as I don't have it :/
Morelet’s Crocodile
Does it make sense?
Expecially for Streaming with Suspense
Britannia PetiteOP
Not really tbh, to my understanding Suspense is replacing certain component, and I specifically need boolean
Morelet’s Crocodile
  const router = useRouter();
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const handleRouteChange = () => {
      setLoading(true); // Set loading to true when route starts changing
    };

    const handleRouteComplete = () => {
      setLoading(false); // Set loading to false when route has finished loading
    };

    router.events.on('routeChangeStart', handleRouteChange);
    router.events.on('routeChangeComplete', handleRouteComplete);
    router.events.on('routeChangeError', handleRouteComplete); // Handle route change errors

    // Cleanup the event listeners
    return () => {
      router.events.off('routeChangeStart', handleRouteChange);
      router.events.off('routeChangeComplete', handleRouteComplete);
      router.events.off('routeChangeError', handleRouteComplete);
    };
  }, [router.events]);
Britannia PetiteOP
Thanks, it wasn't it (router.events is not available) but this gave me an idea. It's as simple as that

  const [isLoading, setIsLoading] = useState(false)

  useEffect(() => {
    setIsLoading(false)
  }, [serverData])


and after updating query params:
setIsLoading(true)
Answer
@Britannia Petite Just tried nuqs but it doesn't seem to be working with nextjs app router data fetching
Brown bear
With nuqs, you can get a loading state with React's useTransition hook, coupled with shallow: false to tell Next.js to re-render the RSC tree:
https://nuqs.47ng.com/docs/options#transitions
@Brown bear With nuqs, you can get a loading state with React's useTransition hook, coupled with `shallow: false` to tell Next.js to re-render the RSC tree: https://nuqs.47ng.com/docs/options#transitions
Britannia PetiteOP
Thanks will give it a shot, previously i was getting some cryptic errors when used with useTransition, but i didn't tried shallow: false
@Britannia Petite Thanks will give it a shot, previously i was getting some cryptic errors when used with useTransition, but i didn't tried shallow: false
Morelet’s Crocodile
Hi man, this is my portfolio site.
https://kozato-portfolio.vercel.app/
I think it would be helpful if we keep contact with each other.
Brown bear
nuqs v1 used to set shallow: false automatically when passing a startTransition option, but when opening up to other React frameworks, this was disabled as they might want to leverage a transition on shallow routing. In practice, no framework seems to support this yet.
@Luis Have you tried nuqs? I’ve heard that’s a great library to sync url state, gives you tools for many use cases
Morelet’s Crocodile
Would you like to accept my friend request? I think it would be helpful if we keep contact with each other.