Created
February 25, 2024 20:37
-
-
Save crutchcorn/5091b9a5dfbd0d89401b3c4aa8f80dd1 to your computer and use it in GitHub Desktop.
Why does this not preserve the `useRef` value between strict mode runs?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateState() { | |
if (!window.executeCount) { | |
window.executeCount = 0 | |
} | |
window.executeCount++ | |
return { | |
count: window.executeCount, | |
} | |
} | |
function App() { | |
const countRef = useRef() | |
if (!countRef.current) { | |
countRef.current = generateState() | |
} | |
const count = countRef.current | |
return <p>{count.count}</p> | |
} | |
createRoot(rootElement).render( | |
<StrictMode> | |
<App /> | |
</StrictMode> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment