Skip to content

Commit

Permalink
Avoid mutating state
Browse files Browse the repository at this point in the history
When knobs change instead of mutating the fields property in state
this assignes a new object as fields using setState.
  • Loading branch information
roonyh committed Aug 30, 2016
1 parent fa87a43 commit 57a3328
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/PropEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ export default class PropEditor extends React.Component {
const { name, value } = change;
const fields = this.state.fields;
const { type } = fields[name];
fields[name].valid = true;
let valid = true;
if (type === 'object') {
try {
eval(`(${value})`); // eslint-disable-line no-eval
} catch (e) {
fields[name].valid = false;
valid = false;
}
}

fields[name].value = value;
this.setState({ fields });
const changedField = {};
changedField[name] = { ...fields[name], ...{ value, valid } };
const newFields = { ...fields, ...changedField };

this.setState({ fields: newFields });
}

createKnob(name) {
Expand Down

0 comments on commit 57a3328

Please sign in to comment.