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

use-callback-state

v1.2.0

Published

use State, but it will callback

Downloads

16

Readme


  • reports when state got updated
  • controls what could be set to the state
  • helps with transformations

useState is about storing a variable, and changing it. However what if not everything could be set, and what if you have to react on state change?

Why?

  • to react on state change in the same tick, not after as usual, causing potential state tearing and inconsistency.
  • for input value validation or transformation

useReducer

  • useCallbackState is quite similar to useReducer, it receives the old state and the new, producing the result. Use reducer does the same, and action could be action. However, you can't replace reducer, while callback in useCallbackStart would always refer to a latest version.

Control

For state validation

import { useCallbackState } from 'use-callback-state';
const [state, setState] = useCallbackState(
  2,
  // allow only even numbers
  (newState, oldState) => (newState % 2 ? oldState : newState)
);

state == 2;

setState(3);
state == 2; // 3 is odd number, the old state value was enforced

setState(4);
state == 4;

For form values management

import { useCallbackState } from 'use-callback-state';
const [value, setValue, forceSetValue] = useCallbackState('John Doe', event => event.target.current);
return <input value={value} onChange={setValue} />;

Report

const [state, setState] = useCallbackState(
  42,
  newState => {
    onValueChange(newState);
  } // not returning anything means "nothing transformed"
);

setState(10);
// call onValueChange(10)

alternative

const [state, setState] = useState(42);
useEffect(() => onValueChange(state), [state]);

// call onValueChange(42) (did you want it?)
setState(10);
// call onValueChange(10)

State separation

One state is "public" - what would be reported to the parent component, and another is "internal" - what user would see. A good example - a DataInput where "public value" could be undefined if it's not valid

const [publicState, setPublicState] = useCallbackState(initialValue, onValueChange);

const [internalState, setInternalState] = useState(publicState);

useEffect(() => {
  setPublicState(isValid(internalState) ? internalState : undefined);
}, [internalState]);

return (
  <>
    <input type="hidden" value={publicState} />
    <input type="text" value={internalState} onChange={e => setInternalState(e.target.value)} />
  </>
);

See also

License

MIT