useUrlQueryState
SourceUse URL query string as state. This is like useState
except the state value
is always an object and the state is stored in the URL.
This hook parses the query string and returns that as an object along with a setter function to transition state values. The setter will transition the URL to match the query params specified.
As different router integrations handle history and navigation differently you can pass the current location and a function to replace the current URL to options.
By default it will work with window.location
and the History API
For react-router:
import { useLocation, useHistory } from 'react-router';function useRouterUrlQueryState(initialState = {}, options = {}) {const location = useLocation();const history = useHistory();return useUrlQueryState({}, { location, replaceUrl: history.replace });}
For navi:
import { useUrlQueryState } from '@prestojs/routing';import { useCallback } from 'react';import { useCurrentRoute, useNavigation } from 'react-navi';export default function useNaviUrlQueryState(initialState = {}, options = {}) {const { url } = useCurrentRoute();const navigation = useNavigation();const replaceUrl = useCallback(nextUrl => navigation.navigate(nextUrl, { replace: true }), [navigation,]);return useUrlQueryState(initialState, {...options,url,replaceUrl,});}
NOTE: Due to the fact that everything in the URL is represented as a string
the returned values in the state object will all be strings. Use options.parse
to do any transforms required on these values.
Parameter | Type | Description | |
---|---|---|---|
* | initialState | __type | The initial state values. If any of the specified keys don't exist in the URL already the URL will be changed such that they do. If all the keys do exist in the URL already this option has no effect. |
options.controlledKeys | Array|true | If specified only these keys will be synced to and read from the request
URL. Any other keys will be ignored. If | |
options.location | __type | The current location. This depends on your router integration. | |
options.params | ParamsEncodeDecode | An object mapping a key name to either a 2-tuple of a decode & encode function or a single decode function. The decode function is used to parse query values to transform them to a certain type or shape. The encode function is used stringify a value to be stored in the URL. When using the single function form (just the decode function) then the encode function is the equivalent of calling the toString() method on the value. Both encode and decode are passed 2 arguments; the value to transform and the name of the parameter. A special key '*' can be set to define the fallback behaviour for all keys not explicitly defined. | |
options.prefix | string | A value to prefix query param keys with. The returned state object contains the un-prefixed keys. If not specified all query params are sync'd. To make this work nicely
with other usages of the hook on the same page consider setting
| |
options.replaceUrl | Function | A function that replaces the current url. This depends on your router integration. |