With useEffect, how can I skip applying an effect upon the initial render?
Asked Answered
S

11

221

With React's new Effect Hooks, I can tell React to skip applying an effect if certain values haven't changed between re-renders - Example from React's docs:

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes

But the example above applies the effect upon initial render, and upon subsequent re-renders where count has changed. How can I tell React to skip the effect on the initial render?

Savaii answered 6/11, 2018 at 19:54 Comment(1)
Have you looked into React.useMemo?Orthognathous
T
235

As the guide states,

The Effect Hook, useEffect, adds the ability to perform side effects from a function component. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in React classes, but unified into a single API.

In this example from the guide it's expected that count is 0 only on initial render:

const [count, setCount] = useState(0);

So it will work as componentDidUpdate with additional check:

useEffect(() => {
  if (count)
    document.title = `You clicked ${count} times`;
}, [count]);

This is basically how custom hook that can be used instead of useEffect may work (updated for the use with strict mode):

function useDidUpdateEffect(fn, inputs) {
  const isMountingRef = useRef(false);

  useEffect(() => {
    isMountingRef.current = true;
  }, []);

  useEffect(() => {
    if (!isMountingRef.current) {
      return fn();
    } else {
      isMountingRef.current = false;
    }
  }, inputs);
}
Theca answered 6/11, 2018 at 21:7 Comment(16)
Where is the suggestion for useRef over setState? I don't see it on this page and I would like to understand why.Mariel
@Mariel This was a comment that was deleted after the answer was updated. The reasoning is that useState woukld result in unnecessary component update.Theca
This approach works, but it violates the react-hooks/exhaustive-deps linter rule. I.e. fn is not given in the array of deps. Anyone have an approach that doesn't violate React best practices?Noaccount
@JustinLang React linter rules != best practices, they only try to address common hook problems. ESLint rules aren't intelligent and may result in false positives or negatives. As long as the logic behind a hook is correct, a rule can be safely ignored with eslint-disable or eslint-disable-next comment. In this case fn shouldn't be provided as input. See the explanation, reactjs.org/docs/… . If something inside fn introduces deps, it's more like they should be provided as inputs directly.Theca
Note: If you are using multiple useEffects that check for didMountRef, make sure only the last one (on bottom) is setting didMountRef to false. React goes through useEffects in order!Graphics
This worked for me but I had to disable React.StrictMode because it renders components twice, which circumvented the intention of the ref guard.Tusche
I made a hook for that called useDidMount() and then I am using it as const didMount = useDidMount(), where didMount is a booleanKamat
This works but if you need a cleanup function it doesn't work for me. Is there a way?, event though we are returning func(); it will not call the cleanup function when using this hook.Tremblay
@Tremblay Can you provide a demo? There's supposed to be no inconsistencies due to the simplicity of code, return fn() just returns cleanup function, considering that fn returns itTheca
Just in case someone needs it in Typescript, the type for fn is EffectCallback and the type for inputs is DependencyList (and is optional in useEffect). Both types can be imported from react.Gabler
This won't work in strict mode, right?Oribel
@BorisBoskovic I didn't notice any problems with strict mode myself, despite the comment above. I wouldn't actually expect them because the hook follows normal lifecycle of a component. If there's a problem associated with it that can be reproduced, please, notify me, I'll try to check.Theca
@EstusFlask Sorry for late response, It actually worked as expected. I was doing it wrong.Tremblay
@EstusFlask the effect will actually run during first render under Strict mode, because react does extra cleanup - setup cycle. Reproduced here: codesandbox.io/s/quirky-tharp-3sdz4x?file=/src/App.jsAutography
ondrej.par, you forgot to use the <Component/> in your sandbox. @EstusFlask I can confirm that this is an issue. React seems to preserve the mutated ref when doing the double cycle in strict mode.Projection
@Projection ondrej.par Thanks for the notice, updated with a safer implementationTheca
O
117

Here's a custom hook that just provides a boolean flag to indicate whether the current render is the first render (when the component was mounted). It's about the same as some of the other answers but you can use the flag in a useEffect or the render function or anywhere else in the component you want. Maybe someone can propose a better name.

import { useRef, useEffect } from 'react';

export const useIsMount = () => {
  const isMountRef = useRef(true);
  useEffect(() => {
    isMountRef.current = false;
  }, []);
  return isMountRef.current;
};

You can use it like:

import React, { useEffect } from 'react';

import { useIsMount } from './useIsMount';

const MyComponent = () => {
  const isMount = useIsMount();

  useEffect(() => {
    if (isMount) {
      console.log('First Render');
    } else {
      console.log('Subsequent Render');
    }
  });

  return isMount ? <p>First Render</p> : <p>Subsequent Render</p>;
};

And here's a test for it if you're interested:

import { renderHook } from '@testing-library/react-hooks';

import { useIsMount } from '../useIsMount';

describe('useIsMount', () => {
  it('should be true on first render and false after', () => {
    const { result, rerender } = renderHook(() => useIsMount());
    expect(result.current).toEqual(true);
    rerender();
    expect(result.current).toEqual(false);
    rerender();
    expect(result.current).toEqual(false);
  });
});

Our use case was to hide animated elements if the initial props indicate they should be hidden. On later renders if the props changed, we did want the elements to animate out.

Organometallic answered 23/5, 2019 at 3:31 Comment(8)
Import changed from react-hooks-testing-library to @testing-library/react-hooks github.com/testing-library/react-hooks-testing-library/releases/…Organometallic
Why did you choose "isMount" and not "didMount" or "isMounted" ?Hutment
Guess I was thinking about whether the current render is the render where the mount was happening. Ya I agree, it sounds a little weird coming back to it now. But it's true on the first render and false after so your suggestions sound misleading. isFirstRender could work.Organometallic
Thanks for the hook! I agree with @ScottyWaggoner , isFirstRender is a better nameGlyn
Thanks for your answer. I am using your hook the way like first value false, after mounting true and renamed didMount :)United
Thank you so much for this ! I totally agree about the name. isFirstRender makes sense. I have used it in my project with this name already =)Penetralia
Recently I used this with Next.js to wait until after hydration to update dates to a local timezone to prevent hydration errors where the client UI didn't match what was server rendered. I named it useIsHydrated, flipped the initial state, and used useState instead of useRef to force a rerender when the value changed.Organometallic
React 18: "no" :/Abecedary
S
39

I found a solution that is more simple and has no need to use another hook, but it has drawbacks.

useEffect(() => {
  // skip initial render
  return () => {
    // do something with dependency
  }
}, [dependency])

This is just an example that there are others ways of doing it if your case is very simple.

The drawback of doing this is that you can't have a cleanup effect and will only execute when the dependency array changes the second time.

This isn't recommended to use and you should use what the other answers are saying, but I only added this here so people know that there is more than one way of doing this.

Edit:

Just to make it more clear, you shouldn't use this approach to solving the problem in the question (skipping the initial render), this is only for teaching purpose that shows you can do the same thing in different ways. If you need to skip the initial render, please use the approach on other answers.

Spires answered 11/4, 2020 at 13:21 Comment(4)
I just learned something. I didn't think this would work, then I tried and it actually does. Thank you!Letendre
React should provide a better way for this, but this issue is open on Github and the proposal is to write a custom solution yourself (which is utter non-sense).Odelsting
The problem with that one is that it will be executed on unMount as well, which is not perfect.Yezd
@ncesar if this was the only answer that worked, probably you are doing something wrong, because the other answer are the correct ones and mine is just for teaching purposSpires
W
37

I use a regular state variable instead of a ref.

// Initializing didMount as false
const [didMount, setDidMount] = useState(false)

// Setting didMount to true upon mounting
useEffect(() => { setDidMount(true) }, [])

// Now that we have a variable that tells us wether or not the component has
// mounted we can change the behavior of the other effect based on that
const [count, setCount] = useState(0)
useEffect(() => {
  if (didMount) document.title = `You clicked ${count} times`
}, [count])

We can refactor the didMount logic as a custom hook like this.

function useDidMount() {
  const [didMount, setDidMount] = useState(false)
  useEffect(() => { setDidMount(true) }, [])

  return didMount
}

Finally, we can use it in our component like this.

const didMount = useDidMount()

const [count, setCount] = useState(0)
useEffect(() => {
  if (didMount) document.title = `You clicked ${count} times`
}, [count])

UPDATE Using useRef hook to avoid the extra rerender (Thanks to @TomEsterez for the suggestion)

This time our custom hook returns a function returning our ref's current value. U can use the ref directly too, but I like this better.

function useDidMount() {
  const mountRef = useRef(false);

  useEffect(() => { mountRef.current = true }, []);

  return () => mountRef.current;
}

Usage

const MyComponent = () => {
  const didMount = useDidMount();

  useEffect(() => {
    if (didMount()) // do something
    else // do something else
  })

  return (
    <div>something</div>
  );
}

On a side note, I've never had to use this hook and there are probably better ways to handle this which would be more aligned with the React programming model.

Wacke answered 29/3, 2019 at 2:13 Comment(2)
useRef is better suited for that because useState will cause an additional and useless render of the component: codesandbox.io/embed/youthful-goldberg-pz3cxAfterworld
you have an error in your useDidMount Function. You have to encapsulate the mountRef.current = true in useEffect() with curly brackets { ...}. Leaving them is like writing return mountRef.current = true, and that cause an error like An effect function must not return anything besides a function, which is used for clean-up. You returned: trueLe
C
30

Let me introduce to you react-use.

npm install react-use

Wanna run:

only after first render? -------> useUpdateEffect

only once? -------> useEffectOnce

check is it first mount? -------> useFirstMountState

Want to run effect with deep compare, shallow compare or throttle? and much more here.

Don't want to install a library? Check the code & copy. (maybe a star for the good folks there too)

Best thing is one less thing for you to maintain.

Chloric answered 31/7, 2021 at 15:6 Comment(6)
Looks like a really good packageSyndactyl
Fantastic resource, so glad I found this!Richardson
Unclear what's the difference between useUpdateEffect & useUpdateEffect? Both run only onceHutment
It's a very bloated library and it's unclear if it was made with tree-shaking in mind. Also it bloats the node_modules if all you need is to run useEffect not on mount...Hutment
Unclear what's the difference between useUpdateEffect & useUpdateEffect? Both run only once Do you mean useUpdateEffect and useEffectOnce? useUpdateEffect is exactly like useEffect but skip the first run. Use can see it here: github.com/streamich/react-use/blob/master/docs/…Chloric
For tree-shaking, you can check here: github.com/streamich/react-use/blob/master/docs/Usage.md Example: You can import like this import useUpdateEffect from "react-use/lib/useUpdateEffect"Chloric
L
9

A TypeScript and CRA friendly hook, replace it with useEffect, this hook works like useEffect but won't be triggered while the first render happens.

import * as React from 'react'

export const useLazyEffect:typeof React.useEffect = (cb, dep) => {
  const initializeRef = React.useRef<boolean>(false)

  React.useEffect((...args) => {
    if (initializeRef.current) {
      cb(...args)
    } else {
      initializeRef.current = true
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, dep)
}
Legume answered 12/5, 2020 at 13:2 Comment(0)
T
5

Here is my implementation based on Estus Flask's answer written in Typescript. It also supports cleanup callback.

import { DependencyList, EffectCallback, useEffect, useRef } from 'react';

export function useDidUpdateEffect(
  effect: EffectCallback,
  deps?: DependencyList
) {
  // a flag to check if the component did mount (first render's passed)
  // it's unrelated to the rendering process so we don't useState here
  const didMountRef = useRef(false);

  // effect callback runs when the dependency array changes, it also runs
  // after the component mounted for the first time.
  useEffect(() => {
    // if so, mark the component as mounted and skip the first effect call
    if (!didMountRef.current) {
      didMountRef.current = true;
    } else {
      // subsequent useEffect callback invocations will execute the effect as normal
      return effect();
    }
  }, deps);
}

Live Demo

The live demo below demonstrates the different between useEffect and useDidUpdateEffect hooks

Edit 53179075/with-useeffect-how-can-i-skip-applying-an-effect-upon-the-initial-render

Tshombe answered 2/2, 2021 at 17:32 Comment(0)
C
4

I was going to comment on the currently accepted answer, but ran out of space!

Firstly, it's important to move away from thinking in terms of lifecycle events when using functional components. Think in terms of prop/state changes. I had a similar situation where I only wanted a particular useEffect function to fire when a particular prop (parentValue in my case) changes from its initial state. So, I created a ref that was based on its initial value:

const parentValueRef = useRef(parentValue);

and then included the following at the start of the useEffect fn:

if (parentValue === parentValueRef.current) return;
parentValueRef.current = parentValue;

(Basically, don't run the effect if parentValue hasn't changed. Update the ref if it has changed, ready for the next check, and continue to run the effect)

So, although other solutions suggested will solve the particular use-case you've provided, it will help in the long run to change how you think in relation to functional components.

Think of them as primarily rendering a component based on some props.

If you genuinely need some local state, then useState will provide that, but don't assume your problem will be solved by storing local state.

If you have some code that will alter your props during a render, this 'side-effect' needs to be wrapped in a useEffect, but the purpose of this is to have a clean render that isn't affected by something changing as it's rendering. The useEffect hook will be run after the render has completed and, as you've pointed out, it's run with every render - unless the second parameter is used to supply a list of props/states to identify what changed items will cause it to be run subsequent times.

Good luck on your journey to Functional Components / Hooks! Sometimes it's necessary to unlearn something to get to grips with a new way of doing things :) This is an excellent primer: https://overreacted.io/a-complete-guide-to-useeffect/

Chalcopyrite answered 6/2, 2021 at 14:21 Comment(0)
S
2

You can use custom hook to run use effect after mount.

const useEffectAfterMount = (cb, dependencies) => {
  const mounted = useRef(false);

  useEffect(() => {
    if (mounted.current) {
      return cb();
    }
    mounted.current = true;
  }, dependencies); // eslint-disable-line react-hooks/exhaustive-deps
};

Here is the typescript version:

const useEffectAfterMount = (cb: EffectCallback, dependencies: DependencyList | undefined) => {
  const mounted = useRef(false);

  useEffect(() => {
    if (mounted.current) {
      return cb();
    }
    mounted.current = true;
  }, dependencies); // eslint-disable-line react-hooks/exhaustive-deps
};

Example:

useEffectAfterMount(() => {
  document.title = `You clicked ${count} times`;
}, [count])
Sketch answered 26/9, 2022 at 15:56 Comment(1)
You have inversed true and false, it's const mounted = useRef(false) and then mounted.current = trueEffusive
A
2

1、I choose the npm package ahooks to solve my issue, you should run npm i ahooks first.
2、Then in your code

  import { useUpdateEffect } from "ahooks";
  useUpdateEffect(() => {
    // do something if deps change but skipping the initial render.
  }, deps);
Arriaga answered 1/8, 2023 at 6:30 Comment(0)
D
0

Below solution is similar to above, just a little cleaner way i prefer.

const [isMount, setIsMount] = useState(true);
useEffect(()=>{
        if(isMount){
            setIsMount(false);
            return;
        }
        
        //Do anything here for 2nd render onwards
}, [args])
Disjointed answered 13/1, 2021 at 11:57 Comment(2)
This is not an ideal way of doing this as setting state causes another renderWinepress
Causes a needless re-render. useRef solution is preferable.Hutment

© 2022 - 2024 — McMap. All rights reserved.