0.0.12 • Published 5 years ago

react-url-state-manager v0.0.12

Weekly downloads
52
License
-
Repository
github
Last release
5 years ago

React URL state manager

React hook for managing state in the URL. Allows you to use the URL as a state for search inputs, page numbers, filters and more. Includes support for debouncing/throttling.

Example

npm i react-url-state-manager

Basic example

In the following example the URL is updated after the user stops typing for more than 250 ms.

import { useUrlState, UrlStateProvider } from 'react-url-state-manager';

const App = () => {
    return <UrlStateProvider>
        <Search />
    </UrlStateProvider>;
};

const Search = () => {
    const defaultUrlState = {
        search: ''
    };

    const [urlState, setUrlState] = useUrlState(defaultUrlState);

    const handleChange = (event) => {
        setUrlState({ search: event.target.value }, { debounce: 250 });
    };
    
    return <form>
        <input type="text" value={urlState.search} onChange={handleChange} />
    </form>;
};

Helper methods

mergeParams(object) creates a query string (for example: '?foo=bar') that merges the object with the current query parameters.

replaceParams(object) creates a query string (for example: '?foo=bar') that replaces the current query parameters with the object.

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago