// React Router v6: hook reads/modifies current location URL query str
import { useSearchParams } from 'react-router-dom'
function App() {
let [searchParams, setSearchParams] = useSearchParams()
const handleSubmit = e => {
e.preventDefault()
let params = serializeFormQuery(e.target)
// assume 'serializeFormQuery()' creates an obj of { key: value }
// pairs from fields in the form that make up the query
setSearchParams(params)
}
return (
<div>
<form onSubmit={handleSubmit}>{/* ... */}</form>
</div>
);
}
import { useSearchParams } from 'react-router-dom'
function Mobile() {
const [search,setSearch]=useSearchParams();
const checkValue = search.get('filter') === 'active';
console.log(checkValue);
return (
<div>
<button onClick={()=>setSearch({filter: 'active'})}>Show Mobile Under 50K</button>
<button onClick={()=> setSearch('')}>Show HighEnd Mobiles</button>
import * as React from "react";
import { useSearchParams } from "react-router-dom";
function App() {
let [searchParams, setSearchParams] = useSearchParams();
function handleSubmit(event) {
event.preventDefault();
// The serialize function here would be responsible for
// creating an object of { key: value } pairs from the
// fields in the form that make up the query.
let params = serializeFormQuery(event.target);
setSearchParams(params);
}
return (
<div>
<form onSubmit={handleSubmit}>{/* ... */}</form>
</div>
);
}
const [searchParams, setSearchParams] = useSearchParams({});
setSearchParams({ hello: "world" });
console.log(searchParams);