Improvements to Typed Router Params
In a previous Byte, I showed the useStrictParams
hook as a way to better manage React router params with better type safety
and ease of use. Since then, I’ve made some improvements and published it
to npm!
You can install the
use-strict-params
library with npm or your favorite package manager.
npm install use-strict-params
A new feature I added since I originally posted was support for optional
properties using the new Optional
function. This is especially useful for
search params where you still want type conversions but the param is indeed
optional.
import { Optional, useStrictSearchParams } from "use-strict-params"
const { page } = useStrictSearchParams({ page: Optional(Number) })