Sets react state to url fragment
import { useCallback } from "react";
import { useUrlState } from "react-state-url-fragment";
export function usePageState<T>(defaultState?: T) {
const getEncodedState = useCallback(() => location.hash.substring(1), []);
const onEncodedState = useCallback((hash) => (location.hash = hash), []);
const handleDecodeError = defaultState && (() => defaultState);
return useUrlState<T>({
getEncodedState,
handleDecodeError,
onEncodedState,
});
}
$ cd demo/
$ make start
Starting the development server...
Check it out here