Mismatch between rendered state and logged state
See original GitHub issueDescribe the bug
👋🏼
I noted today that I was getting mismatches between the logged state and the rendered state in a very simple react
application. I’m probably missing something but I’m at my wits end.
This is only an issue when running the development server. Running the built application does not have this issue.
Did you try recovering your dependencies?
1.22.11
Environment
Environment Info:
current version of create-react-app: 4.0.3
running from /home/sqve/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app
System:
OS: Linux 5.13 Arch Linux
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Binaries:
Node: 14.17.6 - ~/.local/share/nvm/versions/node/v14.17.6/bin/node
Yarn: 1.22.11 - ~/.local/share/nvm/versions/node/v14.17.6/bin/yarn
npm: 7.22.0 - ~/.local/share/nvm/versions/node/v14.17.6/bin/npm
Browsers:
Chrome: Not Found
Firefox: Not Found
npmPackages:
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
react-scripts: 4.0.3 => 4.0.3
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
- Run
npx create-react-app state-mismatch
. - Change the default
App
into:
import { useState } from "react";
const getRandomString = () => Math.round(Math.random()).toString();
export function App() {
const [state, setState] = useState([getRandomString()]);
console.log(state);
return <div className="App">{state.map((x) => x)}</div>;
}
export default App
- Run
yarn run start
and refresh until there is a mismatch.
Expected behavior
The state should always be the same.
Actual behavior
There’s a mismatch between what the console outputs and what is rendered.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Hydration failed because the initial UI does not match ... - GitHub
There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering....
Read more >reactjs - React rendered html does not match console.log
I tried to repro this in a sandbox but it's rendering the same number as it's logging (codesandbox.io/s/react-hooks-playground-forked-sp2x8).
Read more >The Perils of Rehydration - Josh W Comeau
A surprisingly-common misconception can lead to big rendering issues that are difficult to debug. This deep-dive tutorial examines how React ...
Read more >react-hydration-error - Next.js
The first render is called Hydration which is a feature of React. ... is a state passed as a prop there is no...
Read more >Automatic Batching in React 18: What You Should Know
If you observe the browser console, you will see that the "Component Rendering" message is logged only once for both state updates. Now...
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 FreeTop 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
Top GitHub Comments
Hi, I just ran into this problem and found the culprit. I’ll explain because it took me a while. It’s <React.StrictMode>, that create-react-app is probably using under the hood in development mode.
As explained here https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects, StrictMode runs the rendering twice. That’s why you get two different random values. StrictMode also swallows the logs from one of the two renders 😕 that’s why one of the random values is not logged in the console.
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.