React calling a method on load only once

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)