tl;dr; The useSearchParams
hook from react-router
is great as a hybrid state manager in React.
The wonderful react-router has a v6 release coming soon. At the time of writing, 6.0.0-beta.0 is the release to play with. It comes with a React hook called useSearchParams
and it's fantastic. It's not a global state manager, but it can be used as one. It's not persistent, but it's semi-persistent in that state can be recovered/retained in browser refreshes.
Basically, instead of component state (e.g. React.useState()
) you use:
import React from "react";
import { createSearchParams, useSearchParams } from "react-router-dom";
import "./styles.css";
export default function App() {
const [searchParams, setSearchParams] = useSearchParams();
const favoriteFruit = searchParams.get("fruit");
return (
<div className="App">
<h1>Favorite fruit</h1>
{favoriteFruit ? (
<p>
Your favorite fruit is <b>{favoriteFruit}</b>
</p>
) : (
<i>No favorite fruit selected yet.</i>
)}
{["🍒", "🍑", "🍎", "🍌"].map((fruit) => {
return (
<p key={fruit}>
<label htmlFor={`id_${fruit}`}>{fruit}</label>
<input
type="radio"
value={fruit}
checked={favoriteFruit === fruit}
onChange={(event) => {
setSearchParams(
createSearchParams({ fruit: event.target.value })
);
}}
/>
</p>
);
})}
</div>
);
}
To get a feel for it, try the demo page in Codesandbox and note has it basically sets ?fruit=🍌
in the URL and if you refresh the page, it just continues as if the state had been persistent.
Basically, that's it. You never have a local component state but instead, you use the current URL as your store, and useSearchParams
is your conduit for it. The advantages are:
- It's dead simple to use
- You get "shared state" across components without needing to manually inform them through prop drilling
- At any time, the current URL is a shareable snapshot of the state
The disadvantages are:
- It needs to be realistic to serialize it through the URLSearchParams web API
- The keys used need to be globally reserved for each distinct component that uses it
- You might not want the URL to change
That's all you need to know to get started. But let's dig into some more advanced examples, with some abstractions, to "workaround" the limitations.
To append or to reset
Suppose you have many different components, it's very likely that they don't really know or care about each other. Suppose, the current URL is /page?food=🍔
and if one component does: setSearchParams(createSearchParams({fruit: "🍑"}))
what will happen is that the URL will "start over" and become /page?fruit=🍑
. In other words, the food=🍔
was lost. Well, this might be a desired effect, but let's assume it's not, so we'll have to make it "append" instead. Here's one such solution:
function appendSearchParams(obj) {
const sp = createSearchParams(searchParams);
Object.entries(obj).forEach(([key, value]) => {
if (Array.isArray(value)) {
sp.delete(key);
value.forEach((v) => sp.append(key, v));
} else if (value === undefined) {
sp.delete(key);
} else {
sp.set(key, value);
}
});
return sp;
}
Now, you can do things like this:
onChange={(event) => {
setSearchParams(
- createSearchParams({ fruit: event.target.value })
+ appendSearchParams({ fruit: event.target.value })
);
}}
Now, the two keys work independently of each other. It has a nice "just works feeling".
Note that this appendSearchParams()
function implementation solves the case of arrays. You could now call it like this:
{/* Untested, but hopefully the point is demonstrated */}
<div>
<ul>
{(searchParams.getAll("languages") || []).map((language) => (
<li key={language}>{language}</li>
))}
</ul>
<button
type="button"
onClick={() => {
setSearchParams(
appendSearchParams({ languages: ["en-US", "sv-SE"] })
);
}}
>
Select 'both'
</button>
</div>
...and that will update the URL to become ?languages=en-US&languages=sv-SE
.
Serialize it into links
The useSearchParams
hook returns a callable setSearchParams()
which is basically doing a redirect (uses the useNavigate()
hook). But suppose you want to make a link that serializes a "future state". Here's a very basic example:
// Assumes 'import { Link } from "react-router-dom";'
<Link to={`?${appendSearchParams({fruit: "🍌"})}`}>Switch to 🍌</Link>
Now, you get nice regular hyperlinks that uses can right-click and "Open in a new tab" and it'll just work.
Type conversion and protection
The above simple examples use strings and array of strings. But suppose you need to do more more advanced type conversions. For example: /tax-calculator?rate=3.14
where you might have something that needs to be deserialized and serialized as a floating point number. Basically, you have to wrap the deserializing in a more careful way. E.g.
function TaxYourImagination() {
const [searchParams, setSearchParams] = useSearchParams();
const taxRaw = searchParams.get("tax", DEFAULT_TAX_RATE);
let tax;
let taxError;
try {
tax = castAndCheck(taxRaw);
} catch (err) {
taxError = errl;
}
if (taxError) {
return (
<div className="error-alert">
The provided tax rate is invalid: <code>{taxError.toString()}</code>
</div>
);
}
return <DisplayTax value={tax} onUpdate={(newValue) => {
setSearchParams(
createSearchParams({ tax: newValue.toFixed(2) })
);
}}/>;
}
Comments