DEV Community

Cover image for React.js ~Life Cycle~
Ogasawara Kakeru
Ogasawara Kakeru

Posted on

React.js ~Life Cycle~

In React, the life cycle is composed of Mount(create), Update(update) and Unmount(delete).

The life cycle method is defined as those of method.

  • componentDidMount

    • created when the first render once.
    • Used when API connection, DOM manipulation.
  • componentDidUpdate

    • Run when updating DOM.
    • Used in every time updating such as DOM manipulation.
  • componentWillUnmount

    • Run when deleting components.
    • Used in eventListner, timer, and closing API connection.

Side Effect Hook
useEffect hook can replace the life cycle method instead.
useEffect can be used for componentDidMount(runs when generate component), componentDidUpdate(runs every time update) and componentWillUnmount(runs when delete component).

**componentDidMount

useEffect( () => {
    // Runs when initial render
}, []);
Enter fullscreen mode Exit fullscreen mode

**componentDidUpdate

useEffect( () => {
    // Runs every time when rendering
});

Enter fullscreen mode Exit fullscreen mode

**componentWillUnmount

useEffect( () => {
    // Runs every time when rendering
    return (
        // Cleanup something
    );
});
Enter fullscreen mode Exit fullscreen mode
  • The lifecycle refers to the sequence of events through which a React component is created, updated, and eventually destroyed.

  • The useEffect method in hooks allows you to perform operations similar to those of lifecycle methods within a single function.

Top comments (0)