I have a route configured like this:
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{ index: true, element: <Home /> },
{ path: "contacts", element: <Contact /> },
{ path: "info/:link", element: <FooterLinks /> },
{ path: "faq", element: <FAQs /> },
{
path: "search",
element: <SearchResults />,
loader: async ({ params }) => {
// NEED ACCESS TO QUERY PARAMETERS HERE
},
},
{
path: "disease/:id",
element: <DiseaseDisplay />,
loader: async ({ params }) => {
...
},
},
],
},
]);
Is there any way to access the query parameters in the route in the loader function? For example if the route is "localhost:3000/search?searchTerm=aaa"
, how can I access the value of searchTerm
in the loader function?
2
Answers
I believe that gonna help you
The
Route
loader is passed only theRequest
object and the route’s path parameters, e.g.{ params, request }
. If you want to access the URL search string you can access it from therequest.url
value.Example:
or