You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The useSearchParams hook supports functional updates for search params. It feels like there should be parity between search params and state in this regard, so we can do something like this:
import*asReactfrom"react";import{useSearchParams}from"react-router-dom";functionApp(){let[searchParams,setSearchParams]=useSearchParams();functionhandleSubmit(event){event.preventDefault();// The serialize function here would be responsible for// creating an object of { key: value } pairs from the// fields in the form.letparams=serializeFormQuery(event.target);setSearchParams((prevParams)=>{prevParams.set("searchParam",params.searchParam);returnparams;},{state: (prevState)=>{// Only add this one param to state, preserving the rest of it if we can.returnappendToState(prevState,params.stateParam);},});}return(<div><formonSubmit={handleSubmit}>{/* ... */}</form></div>);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The
useSearchParams
hook supports functional updates for search params. It feels like there should be parity between search params and state in this regard, so we can do something like this:Beta Was this translation helpful? Give feedback.
All reactions