Skip to content

useEffect only on update (or only on mount) #40

Open
@gnapse

Description

@gnapse

Even though the documentation says it's a rare thing, I'm bothered by the fact that there's no built-in way to control if you want useEffect to run only on component update, and not on first mount.

I was thinking of creating either this:

useEffectOnUpdate(() => {
  // ...
});

or this:

useEffectEnhanced((isComponentUpdate) => {
  if (isComponentUpdate) {
    // ...
  }
});

on mount

Not to mention that the official way to restrict useEffect to run only on mount is not very clear or self-explanatory at all:

useEffect(() => {
  // ...
}, []);

Maybe I can combine these two gripes to provide these two useEffect alternatives: useEffectOnMount, useEffectOnUpdate. Would also like to provide the third alternative that always run, but receives a flag isComponentUpdate or isComponentMount, but I'm not sure about that, or what to call it (useEffectEnhanced is not very clear in its intention too 😕).

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions