Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-location-state

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-location-state

react hook to the browsers location query state

  • 2.0.1-alpha.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

useQueryState()

npm (tag) Build Status Greenkeeper badge codecov badge GitHub top language lerna

store and retrieve state into/from the browsers location state using modern hooks

Features

  • makes it easy to provide a nice UX to your users, by restoring part of the app state after navigation actions
  • makes it easy to share application in current state
  • supported value types: string | number | boolean | string[]
  • handles stringification and parsing from query string of for supported value types
  • invalid entries from the query string are discarded and the component will receive the default value instead

Installation

yarn add use-location-state

In case you use react-router, please check out the router integration segment.

Usage

The useQueryState() works similar to the useState() hook and returns the current value and a set function in a pair.

The important difference is that you need to pass a name before your default value for your state.

const [value, setValue] = useQueryState('itemName', 'default value')

The name you pass will be used in the query string store the state (after the state was changed).

setValue('different value')

After calling the set function with a new value, the state will be saved withing the query string of the browser, so that the new state is reproducable after reloads or history navigation (using forward / back button).

http://localhost:3000/#itemName=different+value

useQueryState() uses the browsers location.hash property by default. Check out the router integrations to use location.search instead.

Example

import { useQueryState } from 'use-location-state'

function MyComponent() {
  const [active, setActive] = useQueryState('active', true)
  return (
    <div>
      <button type="button" onClick={() => setActive(!active)}>Toggle</button>
      {active && <p>Some active content</p>}
    </div>
  )
}

Try it: Example in CodeSandbox

Example with multiple useQueryState hooks in one component

import { useQueryState } from 'use-location-state'

function MyComponent() {
  const [name, setName] = useQueryState('name', 'Sarah')
  const [age, setAge] = useQueryState('age', 25)
  const [active, setActive] = useQueryState('active', false)
  // ...
}

Try it: Example in CodeSandbox

Router Integration

We plan to provide easy-to-use integrations for all major routers. (more routers soon - work in progress) At the moment we provide integrations for:

react-router

yarn add react-router-use-location-state
import { useQueryState } from 'react-router-use-location-state'

Your favorite router is missing? Feel free to suggest a router.

FAQs

Package last updated on 30 Apr 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc