🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

ComponentDidUpdate

AI & DATA SCIENCE // componentdidupdate

componentDidUpdate is a class component lifecycle method that runs after every re-render caused by a props or state change, except the very first render.

Syntax

componentDidUpdate(prevProps, prevState) {
  // runs after every update, not the initial mount
}

Deep Dive Course

componentDidUpdate fires after every update, a re-render triggered by changed props or state, but critically not after the initial mount, which componentDidMount handles instead. It receives the previous props and previous state as arguments, letting you compare them against this.props/this.state, the current values, to decide whether specific new work, like re-fetching data because a particular prop actually changed, is genuinely needed — this comparison is essential, since without it, code inside componentDidUpdate that unconditionally calls setState or fetches data would run on every single update and risk creating an infinite update loop.

1Understanding ComponentDidUpdate

componentDidUpdate fires after every update, a re-render triggered by changed props or state, but critically not after the initial mount, which componentDidMount handles instead. It receives the previous props and previous state as arguments, letting you compare them against this.props/this.state, the current values, to decide whether specific new work, like re-fetching data because a particular prop actually changed, is genuinely needed — this comparison is essential, since without it, code inside componentDidUpdate that unconditionally calls setState or fetches data would run on every single update and risk creating an infinite update loop.

💡

Always compare the relevant prevProps/prevState value against the current one inside componentDidUpdate before performing conditional work like a data fetch — skipping this check and unconditionally calling setState() inside componentDidUpdate is a classic way to create an infinite re-render loop.

editor.html
class UserProfile extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.userId !== this.props.userId) {
      console.log('userId changed, refetching...');
      fetchUser(this.props.userId).then(user => this.setState({ name: user.name }));
    }
  }
  render() {
    return <p>{this.state?.name ?? 'Loading...'}</p>;
  }
}
localhost:3000

2Practical Example

Here is a real-world application of ComponentDidUpdate showing how it is used in production React code.

editor.html
class BadCounter extends React.Component {
  state = { count: 0 };
  componentDidUpdate() {
    this.setState({ count: this.state.count + 1 }); // BUG: unconditional setState
  }
  render() {
    return <p>{this.state.count}</p>;
  }
}
localhost:3000

3Best Practices

Follow these guidelines when working with ComponentDidUpdate:

1. Compare the relevant prevProps or prevState value against the current value before triggering conditional work like a fetch, to avoid running it on every single update

2. Never call setState() unconditionally inside componentDidUpdate, since that state change itself triggers another update, another componentDidUpdate call, and potentially an infinite loop

3. Use the functional-component equivalent, useEffect(fn, [dependency]), when writing new code, reserving componentDidUpdate for maintaining existing class-based components

⚠️

Tip: Always compare the relevant prevProps/prevState value against the current one inside componentDidUpdate before performing conditional work like a data fetch — skipping this check and unconditionally calling setState() inside componentDidUpdate is a classic way to create an infinite re-render loop.

editor.html
class UserProfile extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.userId !== this.props.userId) {
      console.log('userId changed, refetching...');
      fetchUser(this.props.userId).then(user => this.setState({ name: user.name }));
    }
  }
  render() {
    return <p>{this.state?.name ?? 'Loading...'}</p>;
  }
}
localhost:3000

Examples

Example 01Basic Usage
class UserProfile extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.userId !== this.props.userId) {
      console.log('userId changed, refetching...');
      fetchUser(this.props.userId).then(user => this.setState({ name: user.name }));
    }
  }
  render() {
    return <p>{this.state?.name ?? 'Loading...'}</p>;
  }
}
Example 02Advanced Example
class BadCounter extends React.Component {
  state = { count: 0 };
  componentDidUpdate() {
    this.setState({ count: this.state.count + 1 }); // BUG: unconditional setState
  }
  render() {
    return <p>{this.state.count}</p>;
  }
}

Best Practices

  • Compare the relevant prevProps or prevState value against the current value before triggering conditional work like a fetch, to avoid running it on every single update
  • Never call setState() unconditionally inside componentDidUpdate, since that state change itself triggers another update, another componentDidUpdate call, and potentially an infinite loop
  • Use the functional-component equivalent, useEffect(fn, [dependency]), when writing new code, reserving componentDidUpdate for maintaining existing class-based components

Interview Question

Why must code inside componentDidUpdate compare previous and current props/state before performing conditional work, rather than just checking the current props/state alone?

Hint: Think about whether the current props/state values alone can tell you whether something actually just changed, versus simply what the values currently are.

componentDidUpdate runs after every single update, regardless of which specific prop or state value actually triggered that update, so looking only at the current props or state tells you what their values are right now, but nothing about whether a particular one of them is what actually changed on this specific update versus a previous one. Comparing the previous value, from the prevProps or prevState arguments, against the current value for the exact piece of data you care about is the only way to determine whether that specific value genuinely changed on this particular update, which is essential for guarding conditional work, like a data refetch, that should only happen in response to that specific change, not on every unrelated update that happens to also call componentDidUpdate for a completely different reason.

Exercises

MediumPractice using ComponentDidUpdate in a real scenario.
View Solution
class UserProfile extends React.Component {
  componentDidUpdate(prevProps) {
    if (prevProps.userId !== this.props.userId) {
      console.log('userId changed, refetching...');
      fetchUser(this.props.userId).then(user => this.setState({ name: user.name }));
    }
  }
  render() {
    return <p>{this.state?.name ?? 'Loading...'}</p>;
  }
}

Frequently Asked Questions

Why must code inside componentDidUpdate compare previous and current props/state before performing conditional work, rather than just checking the current props/state alone?

componentDidUpdate runs after every single update, regardless of which specific prop or state value actually triggered that update, so looking only at the current props or state tells you what their values are right now, but nothing about whether a particular one of them is what actually changed on this specific update versus a previous one. Comparing the previous value, from the prevProps or prevState arguments, against the current value for the exact piece of data you care about is the only way to determine whether that specific value genuinely changed on this particular update, which is essential for guarding conditional work, like a data refetch, that should only happen in response to that specific change, not on every unrelated update that happens to also call componentDidUpdate for a completely different reason.

Related Functions

componentdidmountcomponentwillunmountuseeffect