Skip to content

Feature: React custom hook - useDepEffect #117

Open

Description

@yairEO

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

Activity

  1. added this to the 2.0 milestone on May 15, 2023
  2. changed the title [-]React custom hook - useDepEffect[/-] [+]Feature: React custom hook - useDepEffect[/+] on May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    HooksHooks-relatedenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions