I'm learning to use Class
syntax now to create React components now and notice I now have to declare methods like this:
class Foo extends React.Component {
...
bar = () => {
this.setState({ ... })
}
}
instead of like this:
class Foo extends React.Component {
...
bar() {
this.setState({ ... }) // won't work
}
}
or I can't use this.setState()
.
Can anybody explain what's the difference between creating methods like this and how they are related to the function prototype?