Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yano3nora/b246459c8905f31cfda6846bb339a06b to your computer and use it in GitHub Desktop.
Save yano3nora/b246459c8905f31cfda6846bb339a06b to your computer and use it in GitHub Desktop.
Restrict multiple rendering by useRef on React Strict Mode. #js
/**
* useEffect とかで deps array が [] でも
* react strict mode だと multiple rendering で 2 回走って困るので
* ref でなんとかするやつ
*
* @link https://www.sunapro.com/react18-strict-mode/
* @link https://github.com/reactwg/react-18/discussions/18
*/
const initialized = useRef<boolean>(false)
useEffect(() => {
if (initialized.current) {
return
}
// なんらか 1 度しかしたくない処理
initialized.current = true
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment