Bhushan Lodha Bhushan Lodha - 26 days ago 7
React JSX Question

What will be equivalent of following code in es5?

What will be equivalent of following code in es5?

constructor(props) {
super(props);

this.state = { ...this.props };
}

Answer

That code would look something like this without using any >= ES6 syntax.

function MyComponent(props) {
  // super(props)
  React.Component.call(this, props);

  // this.state = { ...this.props };
  this.state = Object.assign({}, props);
}

Babel's site has a repl which you can use to see exactly what the compiled code will look like.

In this case it's quite complex because it's mostly wrapped up in the class utilities that Babel uses to polyfill ES6 classes for ES5.


The second example of this.state = { editFlag : false, ...this.props } would be similar.

this.state = Object.assign({}, editFlag: false, this.props);