npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

with-url-state

v3.0.0-beta.3

Published

[![CircleCI](https://circleci.com/gh/Dean177/use-url-state.svg?style=shield)](https://circleci.com/gh/Dean177/use-url-state) [![codecov](https://codecov.io/gh/Dean177/with-url-state/branch/master/graph/badge.svg)](https://codecov.io/gh/Dean177/with-url-st

Downloads

411

Readme

with-url-state

CircleCI codecov Npm

Lifts the state out of a react component and into the url

color-example

Installation

To install with npm:

npm install with-url-state --save

To install with yarn:

yarn add with-url-state

Usage

Check out the the demo or view the code

The api is very similar to useState with some important differences:

  • Url state must be an object, you can't use any primitive values likes strings, booleans, numbers or collections such as Map or Set.
  • Url values in your state object must be strings as there is no way to tell the if ?someParam=false should be deserialized to a boolean or to a string.
  • The url is shared between your whole app. Multiple components using useUrlState will all affect the url simultaneously. This means that a setUrlState call will cause all components using useUrlState to re-render.
  • State is shallow merged into the previous state e.g.
    newState = {
      ...previousState,
      ...stateUpdate,
    } 
    This allows components to adjust the url without having to worry about 'destroying' state that other components may be relying on.
  • The url is easily edited by your users, you should validate the values you receive

Example

import React from 'react'
import { useUrlState } from 'with-url-state'

export const UrlForm = () => {
  const [urlState, setUrlState] = useUrlState({ color: 'blue' })
  return (
    <div className="UrlForm">
      <div className="current-state" style={{ backgroundColor: props.urlState.color }}>
        <div>{urlState.color}</div>
      </div>
      <div className="color-buttons">
        <button className="Red" onClick={() => setUrlState({ color: 'red' })}>
          Red
        </button>
        <button className="Green" onClick={() => setUrlState({ color: 'green' })}>
          Green
        </button>
        <button className="Blue" onClick={() => setUrlState({ color: 'blue' })}>
          Blue
        </button>
      </div>
    </div>
  )
}

Recipes

Higher-order component

If your application relies on the removed higher-order component API you can re-implement it like so:

import React from 'react'
import { useUrlState } from 'with-url-state'

let withUrlState = (getInitialState, options) => (WrappedComponent) => (props) => {
  let [urlState, setUrlState] = useUrlState(getInitialState(props), options)
  return <WrappedComponent {...props} urlState={urlState} setUrlState={setUrlState} />
}

Render-prop

If your application relies on the removed render prop API you can re-implement it like so:

import React from 'react'
import { useUrlState } from 'with-url-state'

const UrlState = ({initialState, options, render }) => {
  let [urlState, setUrlState] = useUrlState(initialState, options)
  return render({urlState, setUrlState})
}

Motivation

with-url-state automates tiresome query parameter manipulations, simplifying components where the URL will be used for sharing, search results, querying data or tracking a visible portion of a map.

The api provided is:

  • based on hooks
  • type-safe thanks to Typescript
  • very similar to Reacts built in state apis, so converting a component which already manages state is usually as simple as replacing useState with useUrlState!

Pollyfill

For use in IE11 you will need https://github.com/kumarharsh/custom-event-polyfill and add the following to the entry point of your application:

import 'custom-event-polyfill';
if (typeof Event !== 'function') { window.Event = CustomEvent; }  
``