Socket
Socket
Sign inDemoInstall

react-router-6-adapter

Package Overview
Dependencies
5
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-router-6-adapter

Library for convenient work with query parameters


Version published
Maintainers
1
Created

Readme

Source

react-router-6-adapter

This is adapter for use-query-parameters

Note: this not works with react-router versions 6.4 and above

Install

npm i use-query-parameters react-router-6-adapter

or

yarn add use-query-parameters react-router-6-adapter

Example

import { FC } from 'react';

import {
  numberConverter,
  stringConverter,
  useOnQueryParamsChanged,
  useSetQueryParams,
  useQueryParams,
  QueryParamsProvider,
  QueryParamsGlobalProvider,
  stringifyUrl,
} from 'use-query-parameters';

import {
  BrowserRouter,
  Route,
  Routes,
  Link,
  useParams,
} from 'react-router-dom';
import reactRouter6Adapter from 'react-router-6-adapter';

type HomeQueryParams = {
  a?: string;
};

type IdSearchParams = {
  b: number;
};

const Home: FC = () => {
  const setQueryParams = useSetQueryParams<HomeQueryParams>();

  const { params } = useQueryParams<HomeQueryParams, ['a']>('a');

  useOnQueryParamsChanged<HomeQueryParams, ['a']>(['a'], console.log, []);

  const next = `${Math.round(Math.random() * 1000)}`;

  return (
    <>
      <button onClick={() => setQueryParams({ a: next })}>
        Set random query param from {params.a} to {next}
      </button>
      <br />
      <Link to={stringifyUrl('/1', { b: 1 })}>go to id page</Link>
    </>
  );
};

const Id: FC = () => {
  const { id } = useParams<{ id: string }>();

  const { params } = useQueryParams<IdSearchParams, ['b']>('b');

  const next = params.b + 1;

  return (
    <>
      <div>{id}</div>
      <br />
      <Link to={stringifyUrl('/2', { b: next })}>
        Set query param to {next}
      </Link>
    </>
  );
};

const IdPage: FC = () => (
  <QueryParamsProvider<IdSearchParams>
    schema={{
      b: { converter: numberConverter, required: true },
    }}
  >
    <Id />
  </QueryParamsProvider>
);

const HomePage: FC = () => (
  <QueryParamsProvider<HomeQueryParams>
    schema={{
      a: { converter: stringConverter },
    }}
  >
    <Home />
  </QueryParamsProvider>
);

const App: FC = () => (
  <BrowserRouter>
    <QueryParamsGlobalProvider adapter={reactRouter6Adapter}>
      <Routes>
        <Route path='/' element={<HomePage />} />
        <Route path='/:id' element={<IdPage />} />
      </Routes>
    </QueryParamsGlobalProvider>
  </BrowserRouter>
);

License

MIT © Krombik

Keywords

FAQs

Last updated on 19 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc