React-InstantSearch: URL Synchronisation with react-router #6097
Replies: 2 comments 5 replies
-
Have you seen the documentation about routing? https://www.algolia.com/doc/guides/building-search-ui/going-further/routing-urls/react/ |
Beta Was this translation helpful? Give feedback.
-
You need to make use of React Router's navigation hooks and possibly state management within your components to reflect the search query in the URL as a query parameter. Below is a step-by-step guide tailored to your setup: 1. Update URL with the Search Query When the user submits a search query from your custom search box, you need to programmatically navigate to the /search path with the query parameter included. Assuming you're using React Router v5 or v6, you can use the useNavigate hook (v6) or useHistory hook (v5) for this purpose.
2. Read URL Search Params on the Search Page On your search page component, you'll need to read the query parameter from the URL and use it as the initial state or directly in your search component.
3. Update Search Component State Based on URL Ensure your search component's state is updated whenever the URL changes. 🦄 Good Luck. |
Beta Was this translation helpful? Give feedback.
-
Hi!
I cannot find any info on how to get react-router working with URL sync.
I have a SearchPage that we can find under the path: "/search" and the query is passed as a URL param from a custom Algolia component. So when I press enter from the searchbox (the UI components are custom made with hooks) we navigate to the search page but this time with the query as a search param "/search?q=someValue".
Perhaps this is trivial but after reading through the docs I cannot find any way to programmatically do this apart from the routing property on , with which I have seen next router having much discussion but almost nothing on react-router.
Setup:
in header that has
And then when we press enter I navigate with react-router to "/search?q=someValue" to the search page with the URL search param which I then set as the query.
Appreciate any help!
Beta Was this translation helpful? Give feedback.
All reactions