1

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.

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.