const [searchParams, setSearchParams] = useSearchParams({ key: '' })
const [value, setValue] = useState('')
const handleSearch = async () => {
setIsLoading(true)
try {
response = axios.post(...)
} catch (error) {
setError(error)
}
setIsLoading(false)
}
function submitUrlParams() {
handleSearch()
}
function submitForm() {
handleSearch()
setSearchParams({ key: value })
}
useEffect(() => {
const searchParamValue = searchParams.get('key')
if (searchParamValue) {
setValue(searchParamValue)
submitUrlParams()
}
}, [])
I want to submit the form then update the url to match the submit input values.
I also want to use the key value in the url so that when I go to the url, it automatically submits the form with the value of the key. I'm having trouble with useSearchParams. It when going to the url with key: value, the submission value is an empty string.
Is there a better way to achieve my goal? I researched that its good to use a single source of truth...