npm

@breadhead/use-query
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-query

A react hook for easily synchronizing query string (e.g. http://your-url?yourParam=someValue) with react context. Works with any client router through history API.

Installation

yarn add @breadhead/use-query

Usage

It just works

// Navigation.jsx
import { useQuery } from "@breadhead/use-query";

export const Navigation = () => {
  const { param }  = useQuery();

  // ...some code

  return <p>{param}</p>
};

If you want use it with SSR, just add QueryContextProvider

// QueryContextApp.js
import { QueryContextProvider } from "@breadhead/use-query";
import App from './App';

export class QueryContextApp {
  public render() {
    const query = ... // get query from request

    return (
      <QueryContextProvider initial={query}>
        <App />
      </QueryContextProvider>
      );
  };
}

Next.js

If your app builded with Next.js you can use built-in nextWithQuery HOC:

// _app.js
import { nextWithQuery } from "@breadhead/use-query";

export default nextWithQuery(Application);

Package Sidebar

Install

npm i @breadhead/use-query

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

28.4 kB

Total Files

28

Last publish

Collaborators

  • igor.kamyshev
  • iwanttobealight
  • uncleseneca