Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: React custom hook - useDepEffect #117

Open
yairEO opened this issue May 15, 2023 · 0 comments
Open

Feature: React custom hook - useDepEffect #117

yairEO opened this issue May 15, 2023 · 0 comments
Labels
enhancement New feature or request Hooks Hooks-related
Milestone

Comments

@yairEO
Copy link
Contributor

yairEO commented May 15, 2023

useDepEffect (use dependency effect)

Runs a useEffect only when the dependency array changes and not on initial mount.
(see related stackoverflow discussion)

Instead of manually using a ref to know when the component mounted:

const isMountedRef = useRef();

useEffect(() => {
  if (isMountedRef.current)
	document.title = `You clicked ${count} times`;

  isMountedRef = true;
}, [count]); 

Use the custom hook (much shorter):

useDepEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes
@yairEO yairEO added enhancement New feature or request Hooks Hooks-related labels May 15, 2023
@yairEO yairEO added this to the 2.0 milestone May 15, 2023
@yairEO yairEO changed the title React custom hook - useDepEffect Feature: React custom hook - useDepEffect May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Hooks Hooks-related
Projects
None yet
Development

No branches or pull requests

1 participant