1.2.2 • Published 2 years ago

@kikobeats/use-query-state v1.2.2

Weekly downloads
104
License
MIT
Repository
github
Last release
2 years ago

use-query-state

React hook for sharing state via URL.

Installation

$ npm install @Kikobeats/use-query-state --save

Usage

Get query state

Just get it from window.location.search

const [query, setQuery] = useQueryState()

Pass a initial query state

This is especially useful for Server-side Rendering (SSR), when the initial query state will depends on the incoming request.

const [query, setQuery] = useQueryState(ssrQuery)

Transform the query state

Sometimes you want to modify the window.location.search for anything you need for.

const safeDecode = value => {
  try {
    return decodeURIComponent(value)
  } catch (_) {
    return value
  }
}

const [query, setQuery] = useQueryState(undefined, parsedQuery =>
  Object.entries(parsedQuery).reduce((acc, [key, value]) => {
    acc[key] = safeDecode(value)
    return acc
  }, {})
)

Integration

for Next.js

Since Next.js doesn't support ES Modules yet, you need to install next-transpile-modules in order to make possible use the hook on Next.js:

const withTM = require('next-transpile-modules')(['@kikobeats/use-query-state'])

const nextConfig = {}

module.exports = withTM(withOffline(nextConfig))

for Gatsby

import useQueryState from '@kikobeats/use-query-state/gatsby'

License

use-query-state © Kiko Beats, released under the MIT License. Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats

1.2.2

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.2.0-beta.1

3 years ago

1.2.0-beta.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

0.0.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.0

4 years ago