JavaScript What's the purpose of using classes in React? what is a national emergency,whatsapp,whataburger,what time is

I mostly see JavaScript use classes as a constructor as following:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get area() {
    return this.calcArea();
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

What's the reason React uses classes without using the contructor() function, such as following? I don't see classes being used to create instances.

class App extends Component {

    render() {
        return (
            <div className="app-content">
            </div>
        )
    }
}
Answer:1

Right now you should use classes in React if you need to use "advanced" component lifecycle methods like shouldComponentUpdate() or such.

Previously class components were used to handle local state in them. Right now we have Hooks API which allows to use state in a more elegant way and without need of class components.

If you want more details, you can read the article by Dan Abramov: How Are Function Components Different from Classes?.

Regardless your example, you're right, this code:

class App extends Component {

    render() {
        return (
            <div className="app-content">
            </div>
        )
    }
}

can be written as:

function App() {
  return <div className="app-content"></div>
}
Answer:2

I'm creating a web using Bootstrap 3 and I'm using the page-content-wrapper script. When I collapse the page, logo of my web(left right corner in navbar) changes the position due to the padding that ...

I'm creating a web using Bootstrap 3 and I'm using the page-content-wrapper script. When I collapse the page, logo of my web(left right corner in navbar) changes the position due to the padding that ...

I have the following JavaScript object: var example = [{ country: "US", things: { weather: 'cloudy' } }, { country: "US", things: { resource: 'lead', weather:...

I have the following JavaScript object: var example = [{ country: "US", things: { weather: 'cloudy' } }, { country: "US", things: { resource: 'lead', weather:...

Currently I have the following partial code in my puppeteer file: const getImgSrc = await page.$eval('#ldpGallery', el => el.getElementsByTagName('img')); console.log(getImgSrc); The html I'm ...

Currently I have the following partial code in my puppeteer file: const getImgSrc = await page.$eval('#ldpGallery', el => el.getElementsByTagName('img')); console.log(getImgSrc); The html I'm ...

  1. puppeteer eval function

I have created a modal list using bootstrap and Actually I have a form list created and when entered details, it will land me to a desired page. Register button performs this function. But now I have ...

I have created a modal list using bootstrap and Actually I have a form list created and when entered details, it will land me to a desired page. Register button performs this function. But now I have ...