Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Strange double invocation of hook's setState in strict mode #23081

Closed
Zombobot1 opened this issue Jan 8, 2022 · 1 comment
Closed

Bug: Strange double invocation of hook's setState in strict mode #23081

Zombobot1 opened this issue Jan 8, 2022 · 1 comment
Labels
Component: Developer Tools Resolution: Expected Behavior Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Zombobot1
Copy link

React version: 17.0.2

Steps To Reproduce

  1. ???
  2. Invoke set state in the strict mode

Unfortunately, I cannot provide a small reproducible example of my issue. It occurs randomly in some specific component hierarchies. I can provide the problematical code and investigate this issue further if I get at least some hints why it may happen.

This is a simplification of my code:

// in parent component
const [arr, setArr] = useState([])

const appendData = useCallback((data) => {
	console.log('appending')
	setArr((old) => {
		console.log('setting state')
		return [...old, data]
	})
}, [])

// somewhere in children
<button onClick={() => appendData(2)}>Btn</button>

The current behavior

React appends the data twice, however, there is only one 'setting state' printed. I detect second invocation via debugger. The console.log('appending') is printed once.

The expected behavior

'setting state' is printed twice. I may assume that I'm doing something wrong, but I cannot find any explanation why on the second invocation nothing gets printed in console.
I expect that on both invocations the old state is equal. However, it seems like the second invocation gets updated state from the first one.

@Zombobot1 Zombobot1 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jan 8, 2022
@eps1lon
Copy link
Collaborator

eps1lon commented Jan 8, 2022

The second log is disabled in StrictMode by React Devtools. You can disable this behavior in the React Devtools settings > Debugging > Check "Hide logs during second render in StrictMode".

For more information check #22030

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Developer Tools Resolution: Expected Behavior Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants