2.0.0 • Published 9 months ago

@cdxoo/react-router-url-search-params v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@cdxoo/react-router-url-search-params

Hook to access and update url search params in conjunction with react-router. Uses URLSearchParams under the hood.

Requires 'react-router' 5.x and 'react' >= 16.8 as peer dependencies.

Installation

npm install --save @cdxoo/react-router-url-search-params

Usage

import React from 'react';
import { useURLSearchParams } from '@cdxoo/react-router-url-search-params';

// in this component updateQuery pushes the history with
// the updated search params when button is clicked
const MyComponent = () => {
    let [ query, updateQuery ] = useURLSearchParams({ defaults: {
        foo: 'bar',
    }});

    let handleClick = () => {
        updateQuery({ foo: 'baz', other: 'some-value' });
    }

    return (
        <div>
            <button onClick={ handleClick }>click</button>
            <div>{ query.foo }</div>
            <div>{ query.other }</div>
        </div>
    );
}

// you can disable the automatic history push in updateQuery
// in case you want to use the updated search query string elsewhere
// e.g. in <a href='...'>
const MyOtherComponent = () => {
    let [ query, updateQuery ] = useURLSearchParams({ defaults: {
        foo: 'bar',
    }});

    let nextSearchQuery = updateQuery({ other: 'next' }, { push: false })
    return (
        <div>
            <div>{ query.foo }</div>
            <div>{ query.other }</div>
            <a href={ `/my-url/${nextSearchQuery}`}>
        </div>
    )
}
2.0.0-beta.2

9 months ago

2.0.0-beta.1

9 months ago

2.0.0-beta.0

9 months ago

2.0.0

9 months ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago