Skip to content

Conversation

@ekeric13
Copy link

@ekeric13 ekeric13 commented Feb 2, 2018

this.setState now has an updater function that lets you avoid mutating state: https://reactjs.org/docs/react-component.html#setstate

aka:

this.setState((prevState, props) => {
  return {counter: prevState.counter + props.step};
});

Just made changes where I saw it being used incorrectly to keep up with best practices.

@ekeric13 ekeric13 changed the title Update docs to use latest react fiber best practices for this.setState Update docs and tests to use latest react fiber best practices for this.setState Feb 2, 2018

increment() {
this.setState({ count: this.state.count + 1 });
this.setState((prevState) => ({ count: prevState.count + 1 }));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cotainers are not React.Components, while they have same-ish setState method, it doesnt event yet allow callback-style argument - see #1

state = { count: 0 };
increment = () => {
this.setState({ count: this.state.count + 1 });
this.setState((prevState) => ({ count: prevState.count + 1 }));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sam here, this setState method doesn't accept callback style

@Andarist
Copy link
Collaborator

Andarist commented Feb 2, 2018

I feel like this change is really unnecessary at this point of time, this callback-style is also not related to React Fiber itself - you can use callback-style setState in React@15 too.

@jamiebuilds
Copy link
Owner

What we have now is fine

@jamiebuilds jamiebuilds closed this Feb 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants