Skip to content

JulianNymark/use-states-to-next-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

(simple!) React hook to synchronize Next.useRouter() Query with React.useState()

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published