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-flow-hook

v1.0.1

Published

A state management utility for React.

Downloads

2

Readme

useFlow

Get into a flow state when using useFlow to manage state flows.™️

useFlow is a state management utility for React.

const Counter = ({ minimumCount, maximumCount }) => {
  const {
    state: { count },
    actions: { increment, decrement },
  } = useFlow({
    initialState: { count: minimumCount },
    actions: Counter.actions,
    watched: { minimumCount, maximumCount },
  })

  return (
    <div>
      <button onClick={decrement}>-</button>
      <span>{count}</span>
      <button onClick={increment}>+</button>
    </div>
  )
}

useFlow returns an object containing immutable state and a set of functions with the sole power to change that state.

initialState is a self-documenting object containing all properties of the state.

watched contains props, callbacks or other data needed within the actions.

actions returns an object of functions that have the power to mutate the state, described below.

Actions

Counter.actions = ({ getWatched, getState, produceNewState, unmountable, actions }) => ({
  increment: () => {
    const { maximumCount } = getWatched()
    const { count } = getState()
    produceNewState(state => {
      state.count = 
        count >= maximumCount 
          ? maximumCount
          : count + 1
    })
  },

  decrement: () => {
    const { minimumCount } = getWatched()
    const { count } = getState()
    produceNewState(state => {
      state.count = 
        count <= minimumCount 
          ? minimumCount
          : count - 1
    })
  }
})

getWatched returns the watched variables.

getState returns the state variables.

produceNewState accepts a function where the state can be mutated. This is the only way the state can be changed. It is powered by Immer JS.

unmountable allows you to stop execution when your components unmount (which causes React's memory leak warning), and it is documented here.

actions is an object containing all the actions. This allows actions to call other actions, leading to more organized code.

You Might Need useFlow If...

  • Your state from useState is stale and you have no idea how to fix it.
  • useCallback, useMemo and useReducer are giving you a migrane when your task seems like it should be simple.
  • You are resorting to hacks with useRef.
  • Every component you write seems to suddenly drop memory leak warnings without warning.

You Might Not Need useFlow If ...

  • You are not experiencing the problems described above.
  • Your component mostly consumes the data passed to it.
  • You are flowing well with useState and do not need more than that.

useFlow really shines when:

  • Your component or hook is highly asynchronous, like you would see in:
    • A modal counting down the number of seconds until the user's session has expired.
    • A global caching layer supporting invalidation when the user signs out.
  • Your component or hook is highly event driven, like you would see in:
    • A notification component showing a stack of notifications that can be closed one by one.
    • A hook managing the ever-changing state of a form from key presses to validation errors and submission events.

Benefits

A component or hook using useFlow will not need the more complex hooks like useCallback, useMemo, or useReducer, it won't need useState - which can get complicated when asynchronous state is involved - and it will remove most need for useRef. This is not to say those hooks are not worth knowing or using ... but often they feel like obstacles.

useFlow gives you a space to write reactive code, and it gives you a space, in the actions section, to write more traditional event-driven code.

When you have a mix of immutable reactivity and mutable actions - when both sides are really clicking and reinforcing each other - useFlow can feel like rocket fuel for your flow.

Next Steps

  • Install it!

    npm install --save use-flow-hook

    Or with Yarn:

    yarn add use-flow-hook
  • Try the tutorial, which should take around an hour and walks you through the entire API, backwards and fowards, as you build a doggy viewer.

  • Read the tips.