Last active
May 19, 2023 00:22
-
-
Save yano3nora/b246459c8905f31cfda6846bb339a06b to your computer and use it in GitHub Desktop.
Restrict multiple rendering by useRef on React Strict Mode. #js
This file contains hidden or 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
/** | |
* 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