DEV Community

Cover image for Understanding useEffect vs. Class Component Lifecycle Methods in React Native
Amit Kumar
Amit Kumar

Posted on

2 1 1 1 1

Understanding useEffect vs. Class Component Lifecycle Methods in React Native

React Native, like React, allows developers to manage side effects in functional components using Hooks. The useEffect Hook is a powerful tool that replaces lifecycle methods traditionally used in class components. In this blog, we’ll explore the differences between useEffect and class component lifecycle methods in the context of React Native.


1️⃣ useEffect(() => {...}) (Runs on Every Render)

Functional Component:

useEffect(() => {
    console.log("Runs on every render");
  });
Enter fullscreen mode Exit fullscreen mode

Equivalent in Class Component:

class MyComponent extends Component {
  componentDidMount() {
    console.log("Runs on mount");
  }

  componentDidUpdate() {
    console.log("Runs on every update");
  }
}
Enter fullscreen mode Exit fullscreen mode

🔍 Comparison:

  • useEffect runs after every render.
  • In class components, you need both componentDidMount and componentDidUpdate.
  • Functional components provide a more concise syntax in React Native.

2️⃣ useEffect(() => {...}, []) (Runs Only on Mount)

Functional Component:

useEffect(() => {
  console.log("Runs only on mount");
}, []);
Enter fullscreen mode Exit fullscreen mode

Equivalent in Class Component:

class MyComponent extends Component {
  componentDidMount() {
    console.log("Runs only on mount");
  }
}
Enter fullscreen mode Exit fullscreen mode

🔍 Comparison:

  • Both run only once when the component mounts.
  • useEffect is simpler and eliminates the need for componentDidMount.

3️⃣ useEffect(() => {...}, [dependency]) (Runs When a State/Prop Changes)

Functional Component:

useEffect(() => {
  console.log(`State changed: ${count}`);
}, [count]);
Enter fullscreen mode Exit fullscreen mode

Equivalent in Class Component:

class MyComponent extends Component {
  componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count) {
      console.log(`State changed: ${this.state.count}`);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

🔍 Comparison:

  • useEffect runs only when count changes, whereas componentDidUpdate requires manual checks.
  • React Native benefits from the performance improvements of useEffect when handling state changes.

4️⃣ useEffect with Cleanup (Unmounting)

Functional Component:

useEffect(() => {
  const interval = setInterval(() => {
    console.log("Interval running...");
  }, 1000);

  return () => {
    clearInterval(interval);
    console.log("Cleanup on unmount");
  };
}, []);
Enter fullscreen mode Exit fullscreen mode

Equivalent in Class Component:

class MyComponent extends Component {
  componentDidMount() {
    this.interval = setInterval(() => {
      console.log("Interval running...");
    }, 1000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
    console.log("Cleanup on unmount");
  }
}
Enter fullscreen mode Exit fullscreen mode

🔍 Comparison:

  • Functional components use a **return function inside **useEffect for cleanup.
  • Class components require a separate componentWillUnmount method.

useEffect with Multiple Dependencies

Functional Component:

useEffect(() => {
  console.log(`Either count or name changed`);
}, [count, name]);
Enter fullscreen mode Exit fullscreen mode

Equivalent in Class Component:

class MyComponent extends Component {
  componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count || prevState.name !== this.state.name) {
      console.log("Either count or name changed");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

🔍 Comparison:

  • useEffect automatically tracks multiple dependencies.
  • Class components require explicit if conditions for each state/prop.

Image description

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Jetbrains Survey

Calling all developers!

Participate in the Developer Ecosystem Survey 2025 and get the chance to win a MacBook Pro, an iPhone 16, or other exciting prizes. Contribute to our research on the development landscape.

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️