Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Created February 25, 2024 20:37
Show Gist options
  • Save crutchcorn/5091b9a5dfbd0d89401b3c4aa8f80dd1 to your computer and use it in GitHub Desktop.
Save crutchcorn/5091b9a5dfbd0d89401b3c4aa8f80dd1 to your computer and use it in GitHub Desktop.
Why does this not preserve the `useRef` value between strict mode runs?
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