I have a snippet like that, which works:
class Button extends React.Component {
state = { counter : 0 }
handleClick = () => {
this.setState({
counter: this.state.counter+1
})
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
)
}
}
Above, the handleClick
function is an arrow one.
Below, instead I'm trying to use default function (from ES5):
class Button extends React.Component {
state = { counter : 0 }
handleClick = function() {
this.setState({
counter: this.state.counter+1
})
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
)
}
}
ReactDOM.render(<Button/>, mountNode)
The problem that the second snippet don't work properly is in function's writing way? Or somewhere else? React js do not handle default functions?