This ReactJS code is valid and produces the desired output:
import React from 'react'
class MyComponent extends React.Component {
state = {'x': 1}
render() {
console.log('render: this.state:', this.state)
return <div>{this.state.x}</div>
}
}
export default MyComponent
But this very similar ES6 code causes error when running with Node.js:
class MyComponent {
state = {'x': 1}
render() {
console.log('render: this.state:', this.state)
return <div>{this.state.x}</div>
}
}
let c = new MyComponent()
c.render()
Here is the error:
$ node foo.js
/Users/lone/foo.js:2
state = {'x': 1}
^
SyntaxError: Unexpected token =
Why does the second example lead to error when the first example runs fine with ReactJS?