Bug: state could be reset into the initialValue when using StrictMode
See original GitHub issueReact version: 16.13.1
Steps To Reproduce
- Wrap application with
StrictMode
- use
useState
- 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:
- (render1) the
stripe
value set - (render1)
setContext
is called (https://github.com/stripe/react-stripe-js/blob/c97c0ee094605c1d6aa1528f740e7c784e2464a7/src/components/Elements.tsx#L135) - (render2) component rerendered with a correct state
- (render3) component rerendered with incorrect state, equal to the initial one
In the same time:
- data in “refs” is correct
- using a bit different API (promises), moving
setContext
update into nested component, is working as well. (https://github.com/stripe/react-stripe-js/blob/c97c0ee094605c1d6aa1528f740e7c784e2464a7/src/components/Elements.tsx#L148)
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:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.