Skip to content

Mutating state directly #3

@tylercollier

Description

@tylercollier

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions