This is part of the React component:
constructor(props) { super(props); this.state = { counter: 0 }; } handleClick (event) { this.setState((prevState, props) => { counter: props.counter + 1 }); console.log(this.state.counter); }
Trying to execute this page, the browser will read these warning lines in the console:
/Volumes/Development/react-hello-world/src/App.js 17:13 warning Using 'LabeledStatement' is not allowed no-restricted-syntax 17:13 warning Unexpected labeled statement no-labels 17:13 warning 'counter:' is defined but never used no-unused-labels 17:22 warning Expected an assignment or function call and instead saw an expression no-unused-expressions ✖ 4 problems (0 errors, 4 warnings)
I use the counter here: "console.log (this.state.counter);". Why is this error message?
Why if i change
this.setState((prevState, props) => { counter: props.counter + 1 });
with
this.setState({ counter: props.counter + 1 });
works?
source share