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

243
Views
Creating ES6 Class definitions with constructor keyword and this

I've got the following working JavaScript class.I'm hoping there is an ES6 way of doing it without having to use the constructor keyword and also minimize the use of this. I've tried statements like

state = {hasError: false}

and that works for just hasError, but can't figure out how to handle the function bind. I'm hoping not to have to do that like I don't have to use this to access state with the new syntax.

export default class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
    this.updateHasErrorsToFalse = this.updateHasErrorsToFalse.bind(this);
  }

  updateHasErrorsToFalse() {
    this.setState((prevState) => ({
      hasError: false,
    }));
  }

  //state = { hasError: false, error: null,  };

  static getDerivedStateFromError(error) {
    return {
      hasError: true,
      error: error.message
    };
  }

  componentDidCatch(error, errorInfo) {
    // You can also log the error to an error reporting service
    console.log(error, errorInfo);
  }

  updateHasErrorsToFalse() {
    this.setState((prevState) => ({
      hasError: false,
    }));
  }

  render() {
    function addExtraProps(Component, extraProps) {
      return <Component.type {...Component.props} {...extraProps} />;
    }

    if (this.state.hasError) {
      return addExtraProps(this.props.fallback, {
        error: this.state.error,
        resetError: this.updateHasErrorsToFalse,
      });
    }
    return this.props.children;
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

By doing the following, you bind this to function manually.

this.updateHasErrorsToFalse = this.updateHasErrorsToFalse.bind(this);

Instead of that, you can use the arrow function notation which has the lexical binding of this.

  updateHasErrorsToFalse = () => {
    this.setState((prevState) => ({
      hasError: false,
    }));
  }

And you can remove constructor and have state instance as below.

Instead of

 constructor(props) {
    super(props);
    this.state = { hasError: false };
    this.updateHasErrorsToFalse = this.updateHasErrorsToFalse.bind(this);
  }

Just keep following in the class level

state = { hasError: false };
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!