react-state-url-fragment
v2.0.0
Published
[![NPM badge](https://img.shields.io/npm/v/react-state-url-fragment)](https://www.npmjs.com/package/react-state-url-fragment) [![Dependabot badge](https://badgen.net/github/dependabot/iamogbz/react-state-url-fragment/?icon=dependabot)](https://app.dependa
Downloads
6
Readme
React State Url Fragment
Sets react state to url fragment
useUrlState
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,
});
}
Example
Demo
$ cd demo/
$ make start
Starting the development server...
Check it out here