typescriptqwik

Qwik - How to access URL parameters in routeLoader$()?


I want to access the URL parameters with useLocation() in a routeLoader$():

    export const useRouteLoader = routeLoader$(
      (requestEvent: RequestEvent) => {
        const loc = useLocation();
        console.log(loc.params);
        // Other code
      }
    );

But it does not work. I want to log the URL params to the console. How do I do that?


Solution

  • You cannot use useLocation() in routeLoader$()s, but routeLoader$ has access to the RequestEvent API which includes information about the current HTTP request, including the URL params.

    export const useRouteLoader = routeLoader$((requestEvent) => {
      console.log(requestEvent.params);
      //Other code
    });
    

    See more about routeLoader$() at routeLoader$() Qwik City documentation.

    See what the RequestEvent contains at RequestEvent Qwik City documentation.