SyntaxError: unknown: Unexpected token, expected ,

Amit

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.

SALEH

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related