question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom hooks: How to reset state when inputs are updated?

See original GitHub issue

Do you want to request a feature or report a bug?

Probably a feature, I’d like to know why the useState hook does not have a second argument to compare inputs similar to other hooks.

What is the current behavior?

I’m trying to implement custom hooks for a timer and countdown library that we use at work. However, for this example I would like to describe a more minimal example with a counter that counts up from a provided initial value.

I created a small fiddle with a custom hook useCountUp that takes an initial value and increments it each second. The counter should reset itself, when the initialValue that is passed to the custom hook changes. As the fiddle demonstrates, it is currently not really possible to reset the state of the useState hook. As a hack, the hook compares the inputs in a useMemo function and updates the state in the next useEffect run. This however, leads to a duplicate render, which would not be necessary.

My question: Why did the useState hook API not receive a second argument to use reset the state to the provided initalValue when certain inputs change, similar to useMemo, useEffect and other hooks? Without this, we need to re-render twice with the same state in this example to ensure that the counter’s state is consistent.

As a comparison, a Component with render props would have no problems with reseting the state when necessary: https://codesandbox.io/s/3vrpnxo2kq

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React Version: 16.7.0-alpha.2-next

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Nov 20, 2018

What happens to the first render output?

It’s ignored so it doesn’t matter in practice. React won’t re-render deeply. It will immediately call the render again.

0reactions
gaearoncommented, Nov 20, 2018

Hmm. I dunno. It seems a bit niche but I kinda like it? Can you file an RFC please? https://github.com/reactjs/rfcs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reset to Initial State with React Hooks - Stack Overflow
I just wrote a custom hook that returns the actual hooks, along with a resetState function. Usage: const [{ foo: [foo, setFoo], bar:...
Read more >
Reset an input field value in React with the update function ...
Reset an input field value in React with the update function provided by the useState hook | egghead.io.
Read more >
Preserving and Resetting State - React Docs
You can control when to preserve state and when to reset it between re-renders. ... Then React DOM updates the browser DOM elements...
Read more >
Simplifying state initializers with React Hooks - LogRocket Blog
Whenever the reset callback is invoked by the user, you need to update the reset ref count. ... const reset = useCallback(() =>...
Read more >
useForm - reset - React Hook Form
Reset the entire form state, fields reference, and subscriptions. There are optional arguments and will allow partial form state reset.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found