Skip to content

Instantly share code, notes, and snippets.

@dimitrinicolas
Created July 30, 2020 08:39
Show Gist options
  • Select an option

  • Save dimitrinicolas/4241b990d542e1e97cd40b3baba5f921 to your computer and use it in GitHub Desktop.

Select an option

Save dimitrinicolas/4241b990d542e1e97cd40b3baba5f921 to your computer and use it in GitHub Desktop.

Revisions

  1. dimitrinicolas created this gist Jul 30, 2020.
    19 changes: 19 additions & 0 deletions use-effect-not-first.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import { useRef, useEffect, EffectCallback, DependencyList } from 'react';

    /**
    * @param {EffectCallback} effect
    * @param {DependencyList} [deps]
    */
    export const useEffectNotFirst = (effect, deps) => {
    const firstUpdate = useRef(true);

    useEffect(() => {
    if (firstUpdate.current) {
    firstUpdate.current = false;

    return;
    }

    effect();
    }, deps);
    };