Giter VIP home page Giter VIP logo

use-promised-state's Introduction

use-promised-state

NPM

What?

Like useState(), but it doesn't complain when used after unmount.

yarn add use-promised-state
import usePromisedState from 'use-promised-state'

function MyComponent(props) {
  let [state, setState] = usePromisedState(props.initialValue)

  // ...
}

See demo

Why?

When working with Promises in React, you'll sometimes come across errors like this one:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

While there are a number of situations where you do have a memory leak, there's another common situation where this occurs: you've called setState() from a promise handler.

For example:

const [username, setUsername] = useState('')
const [availability, setAvailability] = useState({})

const handleChange = event => {
  const username = event.target.value
  setUsername(username)
  if (username !== availability.username) {
    getUsernameAvailability(username).then(
      // If the component is unmounted before the username's
      // availability is known, then calling `setAvailability`
      // will result in the above error.
      isAvailable => setAvailability({ username, isAvailable })
    )
  }
}

Because JavaScript promises are not cancellable, there's no way to avoid the fact that the handler will eventually be called -- you haven't actually got a memory leak. But you do have an error, and fixing that error has taken a lot of code -- until now.

The usePromisedState() hook is just like useState(). The only difference is that it won't emit an error on the first time that you try to set the state after unmount.

const [availability, setAvailability] = usePromisedState({})

The usePromisedState() hook is a signal that yes, I know that calling setState after unmount doesn't do anything, and I don't care. It makes working with promises easier, and it'll still emit an error if you keep trying to set state after unmount, because that probably does indicate a memory leak.

use-promised-state's People

Contributors

jamesknelson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.