Last active
July 26, 2022 09:29
-
-
Save jeandcr/a0febde8f62bd149440eefcf23d1567e to your computer and use it in GitHub Desktop.
React 18 - Execute on mount, only once. codebase from TanStack Table v8 talk.
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
| import React from "react"; | |
| const doSomething = () => console.log('doing something'); | |
| /** | |
| useEffect(() => { | |
| doSomething(); | |
| return () => cleanup(); | |
| }, [whenThisChanges]); | |
| **/ | |
| export const ExampleComponent: React.FC = () => { | |
| const executedRef = useRef(false); | |
| useEffect(() => { | |
| if (executedRef.current) { return; } | |
| doSomething(); | |
| executedRef.current = true; | |
| }) | |
| return <></>; | |
| } |
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
| import React from "react"; | |
| export const App: React.FC = () => { | |
| useEffect(() => { | |
| // componentDidMount ? | |
| }, []) | |
| useEffect(() => { | |
| // componentDidUpdate ? | |
| }, [onething, anotherthing]) | |
| useEffect(() => { | |
| return () => { | |
| // componentDidUnmount ? | |
| } | |
| }, []) | |
| return <></>; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment