Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutating state directly #3

Open
tylercollier opened this issue Aug 29, 2017 · 0 comments
Open

Mutating state directly #3

tylercollier opened this issue Aug 29, 2017 · 0 comments

Comments

@tylercollier
Copy link

Mutating React state directly is a no-no. We are doing that with the lines in EmployeeEditor.js:

save() {
  this.state.originalEmployee.updateName(this.state.employee.name);
  this.state.originalEmployee.updatePhone(this.state.employee.phone);
  this.state.originalEmployee.updateTitle(this.state.employee.title);
  this.setState({ notModified: true });
  this.props.refreshList();
}

That's why the this.props.refreshList() call is needed. I have never used this.setState(this.state); before. That's needed because we are mutating state behind React's back.

I realize that, to do this properly with a setState, might seem complex to show students. We'd either have to use an immutable library, or make a deep copy. I suppose I'd recommend the latter for now, perhaps using lodash's cloneDeep?

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

No branches or pull requests

1 participant