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

@susisu/use-controlled-state

v0.1.0

Published

A utility for managing controlled local states

Downloads

184

Readme

@susisu/use-controlled-state

CI

npm i @susisu/use-controlled-state
# or
yarn add @susisu/use-controlled-state

Motivation

Suppose we have the following React component.

const NumberInput: React.VFC<{
  value: number;
  onChange: (value: number) => void;
}> = ({ value, onChange }) => {
  return (
    <input
      type="text"
      value={value.toString()}
      onChange={(event) => onChange(parseFloat(event.target.value))}
    />
  );
};

const MyForm: React.VFC = () => {
  const [number, setNumber] = useState(0);
  return <NumberInput value={number} onChange={setNumber} />;
};

Actually this component almost does not work. For example,

  • "" is parsed as NaN and then printed as "NaN", so you cannot clear the input
  • both "0" and "0." represents the same number 0, so you cannot input like "0.123"

There are roughly two ways for solving this problem:

  • change the parent (MyForm) so that it manages the state as a string
  • change the child (NumberInput) so that it manages an additional string state and synchronize with value

Usually the latter is preferable, because it is completely local to the child input, and the parent does not need to know how the child behaves.

@susisu/use-controlled-state provides a utility for managing such local states. With this utility, the above component can be fixed like this:

import { createUseControlledState } from "@susisu/use-controlled-state";

// create a hook
const useControlledNumberString = createUseControlledState(
  (value: number) => value.toString(),
  (state: string) => parseFloat(state)
);

const NumberInput: React.VFC<{
  value: number;
  onChange: (value: number) => void;
}> = ({ value, onChange }) => {
  // use like useState
  const [string, setString] = useControlledNumberString(value, onChange);
  return (
    <input
      type="text"
      value={string}
      onChange={(event) => setString(event.target.value)}
    />
  );
};

const MyForm: React.VFC = () => {
  const [number, setNumber] = useState(0);
  return <NumberInput value={number} onChange={setNumber} />;
};

Usage

createUseControlledState(convert, invert, options?)

  • convert: The function that converts a parent value into a state.
  • invert The function that converts a state into a value for the parent.
  • options.equalValue? The equality function for values. Default is Object.is.
  • options.equalState? The equality function for states. Default is Object.is.

Examples

number and string

const useControlledNumberString = createUseControlledState(
  (value: number) => value.toString(),
  (state: string) => parseFloat(state)
);

Date and string

function stringify(date: Date): string {
  try {
    return date.toISOString();
  } catch {
    return "";
  }
}

function parse(text: string): Date {
  if (text === "") {
    return new Date(NaN);
  } else {
    return new Date(text);
  }
}

function equal(a: Date, b: Date): boolean {
  return Object.is(a.getTime(), b.getTime());
}

const useControlledDateString = createUseControlledState(
  stringify,
  parse,
  { equalValue: equal }
);

License

MIT License

Author

Susisu (GitHub, Twitter)