Reputation: 395
I was going through an online course where the following code works fine on https://jscomplete.com/repl/
But the same code gives me the error.
class Button extends React.Component{
state = {counter: 13};
handleClick = () => {
this.setState((prevState) => {
return {
counter: prevState.counter + 1;
};
});
};
render(){
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
);
};
}
ReactDOM.render(<Button/>, mountNode)
The above returns an error: SyntaxError: unknown: Unexpected token, expected , (7:36)
5 | this.setState((prevState) => {
6 | return {
> 7 | counter: prevState.counter + 1;
| ^
8 | };
9 | });
10 | };
I am not able to find the root cause and fix for this.
Upvotes: 1
Views: 10295
Reputation: 1562
Remove ;
from the statement counter: prevState.counter + 1;
. Javascript allows comma after each key value defintion in an object, not semicolon, that's why you are getting this error
Upvotes: 5