I am trying to create a query link that is created based on the state of the component. When user selects an option the states are changed and then user can select ‘search’ button. The query on the search button should however be according to the states that were changed by the user.
For eg.
/rent/?bedrooms=1&bathrooms=1
/rent/?bedrooms=2&parking=1
const FilterOption = () => {
const [postCode, setPostCode] = useState();
const [price, setPrice] = useState();
const [bedroomCount, setBedroomCount] = useState(0);
const [bathroomCount, setBathroomCount] = useState(0);
const [parkingCount, setParkingCount] = useState(0);
}
2
Answers
instead of query params can you use the react router state? something like
then to use it.
Use the
useSearchParams
hook to take the current state values and update the URL search params.Example:
If you need a little more control over the search params you can check the counts and remove params where the count is 0.