use-states-to-next-query
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

⚠️ You probably want nuqs ⚠️

https://www.npmjs.com/package/nuqs is a much more polished & popular package that does the same thing (but better)! I couldn't initially find it before this package was created. One small difference I could see is that nuqs requires you to use client (and disable ssr by dynamically loading it or get hydration errors when linking a page with query params) whereas this package does everything within useEffect so it's forced to be purely client-side. I'll keep this package in existence purely for its simplicity, but I highly recommend you use nuqs over this.

Usage

Pass the return value from React.useState() into the value of a Record / dict. The key becomes the query parameter used. the router passed in is a Nextjs Pages router.

const router = useRouter();

const search = useState("");
const chips = useState<string[]>([]);

const [currentSearch, setSearch] = search;
const [currentChips, setChips] = chips;

useStatesToNextQuery(router, {
  search,
  chips,
});

setSearch('testinput')
setChips(['testinput1', 'testinput2'])

In the example above, the resulting query params will be the following:

?search=testinput&chips=testinput1&chips=testinput2

Any changes you make to the states will now update in the URL.

Package Sidebar

Install

npm i use-states-to-next-query

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

5.76 kB

Total Files

4

Last publish

Collaborators

  • just_a_guy