We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Hello, I'm confused about how useState and setState work, for a simple example:
import React, { useState } from 'react'; const globalLog = console.log.bind(window); function App() { const [count, setCount] = useState(0); function handleClick() { setCount((c) => { console.log('local log'); globalLog('global log'); return c + 1; }); } return ( <div className="App"> <p>{count}</p> <button onClick={handleClick}>add</button> </div> ); }
You can use this prepared code example:https://codesandbox.io/s/goofy-sea-6evwt?file=/src/App.js
React StrictMode:
local log
global log
General mode:
And I know React uses a cycle linked-list to store the updater,and call all updaters in a loop during the update phase the source code,
So in strict mode, what does React do to the 'local' console.log and why does it only print once.
console.log
Is this a bug or a question ?
The text was updated successfully, but these errors were encountered:
In StrictMode React rerenders the app two times (in dev mode) to detect and warn if there are side-effects while rendering. So as long as there are no sideeffects in render,setState etc it should be fine. https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects
Sorry, something went wrong.
@a-c-sreedhar-reddy thank you, this is a problem not a bug, this issus can be closed.
Related PRs:
Double-render function components with Hooks in DEV in StrictMode
Disable console.logs in the second render pass of DEV mode double render
We've changed the logging behavior to be more intuitive in React 18. #21783 (comment)
No branches or pull requests
Hello, I'm confused about how useState and setState work, for a simple example:
You can use this prepared code example:https://codesandbox.io/s/goofy-sea-6evwt?file=/src/App.js
React StrictMode:
local log
andglobal log
print once each.local log
prints once, butglobal log
prints twice.General mode:
local log
andglobal log
print once each.And I know React uses a cycle linked-list to store the updater,and call all updaters in a loop during the update phase the source code,
So in strict mode, what does React do to the 'local'
console.log
and why does it only print once.Is this a bug or a question ?
The text was updated successfully, but these errors were encountered: