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

@casekit/use-location-state

v0.0.3

Published

Provides a typed and validated way of interacting with location.state

Downloads

4

Readme

@casekit/use-location-state

A hook to make it easier to interact with the browser's location.state in a typesafe, validated way (using zod).

The first argument to the hook is object describing the properties the component is interested in as zod schemas.

The second argument is a set of default values. Any values that are not optional in the schema must be provided (and you'll get a type error if you don't). If no values are required in the schema, you don't need to pass a second argument.

import { useLocationState } from "@casekit/use-location-state";

export const MyComponent = () => {
    const locationState = useLocationState(
        { foo: z.string(), bar: z.number().optional() },
        { foo: "hello" },
    );

    return (
        <button onClick={() => locationState.update({ foo: "goodbye" })}>
            {locationState.value.foo}
        </button>
    )
};

The returned object has a consistent object identity while the state stays the same. It contains a number of keys:

  • value - the current value of the state
  • update - a function that allows you to update the state
  • replace a function that allows you to completely replace the state
  • clear a function that allows you to reset the state to its default values

The value key contains only properties specified in the schema, and will be typed based on the schema. Other properties in the state will still be there for use by other components, but they will not be included in the return value of this hook unless specified in the schema.

The update function applies its changes to the state - any keys it does not specify (including any extra keys not specified in the schema) will be unaffected.

The replace and clear functions should be used with caution, as they apply their changes to the entire of location.state, including keys not specified in the schema. These are mostly helpful for actions that completely change the context of the page.

The hook can be used multiple times by different components with different schemas. As long as they don't use the same keys for different data, this should work fine.