Parsers
Custom parsers
Making your own parsers for custom data types & pretty URLs
You may wish to customise the rendered query string for your data type.
For this, nuqs
exposes the createParser
function to make your own parsers.
You pass it two functions:
parse
: a function that takes a string and returns the parsed value, ornull
if invalid.serialize
: a function that takes the parsed value and returns a string.
Caveat: lossy serializers
If your serializer loses precision or doesn’t accurately represent the underlying state value, you will lose this precision when reloading the page or restoring state from the URL (eg: on navigation).
Example:
Here, setting a latitude of 1.23456789 will render a URL query string
of lat=1.2345
, while the internal lat
state will be correctly
set to 1.23456789.
Upon reloading the page, the state will be incorrectly set to 1.2345.