All Projects → baruchiro → use-route-as-state

baruchiro / use-route-as-state

Licence: MIT license
Use React Router route and query string as component state

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
Dockerfile
14818 projects
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to use-route-as-state

ReSift
A state management library for data fetches in React
Stars: ✭ 39 (+5.41%)
Mutual labels:  hook, react-hooks
react-hook-layout
Layouts in React.
Stars: ✭ 16 (-56.76%)
Mutual labels:  hook, react-hooks
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-45.95%)
Mutual labels:  hook, react-hooks
web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+2440.54%)
Mutual labels:  hook, react-hooks
use-dencrypt-effect
⚛ A custom React hook generating crypting text effect.
Stars: ✭ 39 (+5.41%)
Mutual labels:  hook, react-hooks
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+7664.86%)
Mutual labels:  hook, react-hooks
use-react-router-breadcrumbs
tiny, flexible, hook for rendering route breadcrumbs with react-router v6
Stars: ✭ 170 (+359.46%)
Mutual labels:  hook, react-router
Fakeflix
Not the usual clone that you can find on the web.
Stars: ✭ 4,429 (+11870.27%)
Mutual labels:  react-router, react-hooks
window-scroll-position
React hook for Window scroll position
Stars: ✭ 81 (+118.92%)
Mutual labels:  hook, react-hooks
formalizer
React hooks based form validation made for humans.
Stars: ✭ 12 (-67.57%)
Mutual labels:  hook, react-hooks
react-use-countdown
React hook for countdown state.
Stars: ✭ 19 (-48.65%)
Mutual labels:  hook, react-hooks
go-qs
A Go port of Rack's query string parser
Stars: ✭ 96 (+159.46%)
Mutual labels:  url, querystring
covid
COVID-19 updates webpage
Stars: ✭ 117 (+216.22%)
Mutual labels:  react-router, react-hooks
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (+0%)
Mutual labels:  hook, react-hooks
react-social-network
react social network
Stars: ✭ 13 (-64.86%)
Mutual labels:  react-router, react-hooks
react-use-hotkeys
React hook for creating simple keyboard shortcuts
Stars: ✭ 74 (+100%)
Mutual labels:  hook, react-hooks
react-guidebook
📚 React 知识图谱 关于概念、技巧、生态、前沿、源码核心
Stars: ✭ 22 (-40.54%)
Mutual labels:  react-router, react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+170.27%)
Mutual labels:  react-router, react-hooks
useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (+375.68%)
Mutual labels:  hook, react-hooks
Fre
👻 Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+8535.14%)
Mutual labels:  hook, react-hooks

use-route-as-state

Use React Router route and query string as component state

npm npm Release Github Pages

Install

npm install --save use-route-as-state

Usage

You can see a live demo, including code, here.

// URL: /:param?query=
import * as React from 'react'

import { useRouteParams, useQueryString } from 'use-route-as-state'

const Example = () => {
  const [{ param }, setRouteParams] = useRouteParams()
  const [{ query }, setQueryParams] = useQueryString()

  return (
    <div>
      <input
        value={ param }
        onChange={({ target }) => setRouteParams({ param: target.value })} />
      <input
        value={ query }
        onChange={({ target }) => setQueryString({ query: target.value })} />
    </div>
  )
}

API

This library is trying to behave like the useState React hook, by exposing a similar interface.

type DispatchState<TState> = Dispatch<SetStateAction<TState>>
type RouteObject = Record<string, string>

useRouteParams

Type: useRouteParams: (defaultValues?: RouteObject): [RouteObject, DispatchState<RouteObject>]

Use to sync the URL Parameters with you component.

This custom hook returns an array with two elements:

  • The first element is a string to string object, when the key is the route param name, and the value is the value of this param.

  • The second element is a function to update the route with new string to string object. Like in useState, you can set a new object, or set a function to transaform the prev state to a new one.

Updating the route will push the updated route to the history.

The params object will be reactive to the route. It means the any time the route changed, the params object (the first element from useParamsAsState) will change according to the route and will render the component.

The update function (the second element from useParamsAsState) will change the route, and it will cause an update in the params object, respectively.

Note

To use Route Params, you have to declare the params with the React Router API.

useQueryString

Type: useQueryString: (defaultValues?: RouteObject): [RouteObject, DispatchState<RouteObject>]

Use to sync the Query Parameters with you component.

This hook works just like useParamsAsState, except you don't need to declare any special route in the React Router. You can use this hook in any component, down in the tree, as long as there is a Router somewhere up in the tree.

Updating the route will replace the updated route to the history.

useQueryStringKey

Type: useQueryStringKey: (key: string, defaultValue?: string): [string | undefined, Dispatch<SetStateAction<string>>]

Instead of managing the whole query object, you can use this to get a reactive reference to the value itself.

Example:

// URL: /?foo=bar
import * as React from 'react'
import { useQueryStringKey } from 'use-route-as-state'

const Example = () => {
  const [foo, setFoo] = useQueryStringKey('foo')

  return (
    <div>
      <input
        value={ query }
        onChange={({ target }) => setFoo(target.value)} />
    </div>
  )
}

useUrlState

type UrlState = {
  params: RouteObject,
  query: RouteObject
}

Type: useUrlState: (defaultValues?: UrlState): [UrlState, DispatchState<UrlState>]

Due to limitations in React Router, and React itself, you can't use different hooks here together during one render cycle.

In order to solve that, you can use this hook to control both route params and query string at once.

Development

Local development is broken into two parts (ideally using two tabs).

First, run rollup to watch your src/ module and automatically recompile it into dist/ whenever you make changes.

yarn start # runs rollup with watch flag

The second part will be running the example/ create-react-app that's linked to the local version of your module.

# (in another tab)
cd example
yarn start # runs create-react-app dev server

Now, anytime you make a change to your library in src/ or to the example app's example/src, create-react-app will live-reload your local dev server so you can iterate on your component in real-time.


This hook is created using create-react-hook.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].