0.0.7 • Published 25 days ago

use-url-search-state v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
25 days ago

use-url-search-state

use-url-search-state is a dependency-free React Hook library that allows you to save and sync application state using the URL's search parameters

Installation

npm install --save use-url-search-state

How to Use

import React from 'react';
import { useURLSearchState } from 'use-url-search-state';

function Page() {
    //                                          default value, will change url seacrh to "?type=first"
    const [state, setState] = useURLSearchState({ type: 'first' });

    return (
      <div>
        <h3>{state.type}</h3>
        <button
            onClick={() => setState({ type: 'first' })}
        >first</button>
        <button
            onClick={() => setState({ type: 'second' })}
        >second</button>
      </div>
    );
  }
}
import React from 'react';
import { useURLSearchState } from 'react-url-search';

function Page() {
    const [state, setState] = useURLSearchState(
        {
            count: 0
        },
        {
            // ensure the type of count is Number
            count: { type: Number }
        }
    );

    return (
      <div>
        <h3>{state.count}</h3>
        <button
            onClick={() => setState({ count: state.count + 1 })}
        >add</button>
        <button
            onClick={() => setState({ count: state.count - 1 })}
        >minues</button>
      </div>
    );
  }
}
0.0.7

25 days ago

0.0.6

5 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

11 months ago