Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 1.79 KB

README.md

File metadata and controls

47 lines (34 loc) · 1.79 KB

React State Url Fragment

NPM badge Dependabot badge Dependencies Build Status Coverage Status

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

$ cd demo/
$ make start

Starting the development server...

Check it out here