Skip to content

Instantly share code, notes, and snippets.

@jeandcr
Last active July 26, 2022 09:29
Show Gist options
  • Select an option

  • Save jeandcr/a0febde8f62bd149440eefcf23d1567e to your computer and use it in GitHub Desktop.

Select an option

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.
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 <></>;
}
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