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

react-hooks-use-form-state

v0.0.1

Published

The missing React Hook to handle form initial values that depend on dynamic data from SWR, React Query, and more

Downloads

27

Readme


The Problem

You use a data fetching libraries like useSWR() and useQuery() and want to set initialState dynamically after some data has been fetched.

Explanation

Suppose you have an index page and an edit page for some products.

When you navigate to an edit page from /products to /products/:id/edit, ideally you want to write code like this and call it a day.

const opts = { suspense: true }

const { data: product } = useSWR(`/products/${id}`, opts)
const [name, setName] = useState(product.name)
// ...

However, the above code does NOT work properly.

If product name has changed and you have a stale cache, useSWR() will return stale data on first render and your state will not be updated after revalidation.

Evne if you don't use suspense and split components into parent / child, you still have to handle the problem of staleness somehow. What to do?

Related discussions of the problem

The Solution

Use useForm() to automatically handle updates to initialValue until user starts editing the value.

const options = { suspense: true }
const { data: product } = useSWR(`/products/${id}`, options)
const [name, setState] = useForm(product.name)

// or even without suspense you can write almost exactly the same
const { data: product } = useSWR(`/products/${id}`)
const [name, setName] = useForm(product?.name ?? '')

// ...

How does it work?

The simplified version of the hook is like this:

function useForm(initialValue) {
  const [isChanged, setIsChanged] = useState(false)
  const [state, setState] = useState(undefined)
  return [
    isChanged ? state : initialValue,
    (state) => {
      setIsChanged(true)
      setState(state)
    },
  ]
}

In addition to the above, this library supports TypeScript, functional state updater pattern, and a reset function to revert back to initialState.

import { useFormState } from 'react-hooks-use-form-state'
const [state, setState, reset] = useFormState(dynamicData)
setState((prevState) => nextValue)

// For example, you can clear state on modal open
const onModalOpen = { reset() }

Installation

yarn add react-hooks-use-form-state

or

npm install react-hooks-use-form-state