Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React calling a method on load only once

Tags:

reactjs

I'm new to Hooks and have been encountering some cases that have been sending me chasing my tail.

Hope someone can explain or provide solutions that make sense to me:

  1. Loading a method only once on mount of a component is confusing. I tried this method, it works, but don't understand it. Can someone explain this to me:

    const useMountEffect = fun => useEffect(fun, [])
    
    useMountEffect(() => {
      if (!isEdit) handleAddRow()
    })
    
    const handleAddRow = () => {
      let appendArr = [...listArr, '']
      setListArr(appendArr)
    }
    

Following this thread: How to call loading function with React useEffect only once

I tried just using useEffect without dependency and eslint does not like that and they recommend adding a skip next line which seems kind of hacky:

// eslint-disable-next-line
like image 330
David Pell Avatar asked Sep 25 '19 14:09

David Pell


People also ask

How do you call a function only once on page load React?

Use the useEffect hook to only call a function once in React. When the useEffect hook is passed an empty dependencies array, it is only ran when the component mounts. This is the preferred approach when you have to fetch data when the component mounts.

How do you make use effect run only once?

Side Effect Runs Only Once After Initial Render You do not want to make this API call again. You can pass an empty array as the second argument to the useEffect hook to tackle this use case. useEffect(() => { // Side Effect }, []); In this case, the side effect runs only once after the initial render of the component.

How do you call a method on load in React?

The useEffect runs by default after every render of the component. When placing useEffect in our component we tell React that we want to run the callback as an effect. React will run the effect after rendering and after performing the DOM updates.


Video Answer


2 Answers

If I'm correct you want something similar to componentDidMount life-cycle method. The way to do that is

function MyComponent(props){
    useEffect(()=>{
        // do stuff here...
    }, []) // <-- empty dependency array
    return <div></div>
}

To understand what's happening you'll need to understand how the useEffect hooks work. Using the useEffect hook without any dependencies will trigger the effect every time some state or prop changes and causes a re-render. but if we pass an empty array as a dependency it will be as the effect not dependent on anything else, so it will only trigger when the component mounts.

like image 128
Prithwee Das Avatar answered Oct 16 '22 07:10

Prithwee Das


The empty array [] argument tells useEffect() to run only once

It will run only once on mount, like componentDidMount used to

Explanation:

useEffect() takes 2 arguments - your function and an array of dependencies

useEffect(
    yourFunction, // <- function that will run on every dependency update
    [] // <-- empty dependency array
) 

The dependency array argument tells it when to run your function. If the dependencies don't change it won't run again. If dependencies do update it will run your function again.

If you pass in an empty array (no dependencies) then the function will only run on mount.

ESLint missing dependency error

React Hook useEffect has a missing dependency: 'xxx'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

The reason for the ESLint rule is that useEffect() is designed to run on every dependency update it will expect you to add all the dependencies to that array.

To ignore this you can add above the dependency array line:

// eslint-disable-next-line react-hooks/exhaustive-deps
like image 45
Jonathan Irwin Avatar answered Oct 16 '22 09:10

Jonathan Irwin