Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Why and how is state being set this way inside `getDerivedStateFromError`?

I'm learning about ErrorBoundaries in React and was wondering why and how in the getDerivedStateFromError method, is state being set this way?

// REACT
import React from 'react';

export class ErrorBoundary extends React.Component {

  state = {
    isThereError: false
  }

  static getDerivedStateFromError(error) {
    return {
      isThereError: true 
    };
  }

  // ...

};

Shouldn't it be:

// REACT
import React from 'react';

export class ErrorBoundary extends React.Component {

  state = {
    isThereError: false
  }

  static getDerivedStateFromError(error) {
    
    this.setState({ isThereError: true })

  }

  // ...

};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

State isn't being set in the method itself. But by convention the framework expects that method to return an updated state object. The framework then internally uses that result to update state:

This lifecycle is invoked after an error has been thrown by a descendant component. It receives the error that was thrown as a parameter and should return a value to update state.

So when implementing getDerivedStateFromError in your component you don't need to worry about updating state within that method. Just return the new state.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!