Keeps showing the message "Build Error, open.your browser console to learn more". Not a single is passing yet

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

class Counter extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    count: 0
  };
  // Change code below this line
 this.incrememt = this.increment.bind(this);
 this.decrement = this.decrememt.bind(this);
 this.reset = this.reset.bind(this);
  // Change code above this line
}
// Change code below this line
 increment() {
   this.setState(function(state) {
     return {count: state.count + 1}
   });
 };
decrement() {
this.setState(state => {state.count - 1});
};
reset() {
this.setState(state => {state.count = 0});
};

// Change code above this line
render() {
  return (
    <div>
      <button className='inc' onClick={this.increment}>Increment!</button>
      <button className='dec' onClick={this.decrement}>Decrement!</button>
      <button className='reset' onClick={this.reset}>Reset</button>
      <h1>Current Count: {this.state.count}</h1>
    </div>
  );
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 5.1.1; SM-J200G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36

Challenge: Write a Simple Counter

Link to the challenge:

Ahhh, took me a minute. I was trying to figure why the increment was passing, but the other two weren’t. And it’s a fun challenge - by using fat-arrow functions here, you are also using the single-line return functions within the setState in those two. But that’s where the problem are occuring.

Where you have this:

this.setState(state => {state.count - 1});

…javascript is seeing the state => { state.count -1} as a function body. Wrap the {...} in parentheses, to indicate that you’re returning an object. Additionally, the property you’re setting is not called state.count, but count. So you might want to do something like this:

decrement() {
  this.setState( state => ({count: state.count -1}) )
}

So the object being returned has a count property, but it’s based on the previous state’s count property.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.