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.

Bug: state could be reset into the initialValue when using StrictMode

See original GitHub issue

React version: 16.13.1

Steps To Reproduce

  1. Wrap application with StrictMode
  2. use useState
  3. sometimes value would be reset to the initial

Link to code example: https://codesandbox.io/s/react-stripe-js-z79qw?file=/src/index.js

There are two cases, one is not working in StrictMode and one works well. Without strick mode both works. Console output is empty.

We actually found the buggy behaviour in stripe-js/react - it uses useState to save the context value. And it is (not) working this way:

In the same time:

The current behavior

I’ve failed to understand the current behavior - there is a case (we know one) when state is reset, while there are many cases where everything is working as expected.

The expected behavior

State should be always preserved; StrictMode should have to effect on hooks.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
theKasheycommented, Jun 9, 2020

Anyway, look like this is not a bug, but a feature. I consider my question as fully answered and the problem is absolutely clear now. It’s not only “not right”, but actually “not safe” to base any logic on refs. If one could do it other, probably immutable way,- they should.

1reaction
eps1loncommented, Jun 9, 2020

But one could monkey patch useState and detect situations like this.

You don’t know where this value comes from. It could be safe e.g. from a mutable source or context. React itself might be able to do that because it could know that a second setState was triggered from strict mode.

But in concurrent mode it’s valid to call setState with different values between renders.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to use useState initial value as function? - Stack Overflow
You use it when you want the computation of that initial state to happen only once. Because if you use an expression instead...
Read more >
Hooks API Reference - React
Returns a stateful value, and a function to update it. During the initial render, the returned state ( state ) is the same...
Read more >
How to Manage State in a React App – With Hooks, Redux ...
Hi! In this article we'll take a look at the many ways you can manage state in a React app. We'll start by...
Read more >
reset initial state of children component - You.com | The AI ...
I'm having an issue where, whenever I update my context state within a hook (useEffect, useCallback, etc.), any other state updates that I...
Read more >
Using strict mode in React 18: A guide to its new behaviors
Similarly, strict mode in React is a development-only tool that enforces stricter warnings and checks as you write React code. You can enable ......
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