Using state in React

Hi,

I was going through the React course and I noticed that although in the lesson “Use State To Toggle An Element” https://www.freecodecamp.org/learn/front-end-libraries/react/use-state-to-toggle-an-element
It’s mentioned that this is a bad practice:

this.setState({
  counter: this.state.counter + this.props.increment
});

and it should be done this way:

this.setState((state, props) => ({
  counter: state.counter + props.increment
}));

In following lessons, the first way is used, like in the lesson " Render Conditionally from Props"

Am I missing something?

1 Like

Just because something is bad practice doesn’t mean you’ll never see it used.
Generally when something is referred to as “clean code” it means that’s the industry standard, and others will be more likely to help you out/understand your code.

True! But then why even bother mentioning it when most following lessons use the “non-clean” method, maybe they should follow the pattern they teach.
Sorry for being so pedantic… Just saw this and I wanted to make sure that I didn’t misunderstand something.

1 Like

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.