Skip to content
Permalink
Browse files
Moved the logic for adding a new list item into new utility method 't…
…his._mutateStateToAddItem(...)'.
  • Loading branch information
maratbn committed Aug 5, 2018
1 parent 19d01ff commit 5bd1dc34c858b3ff7939df405471bab6ce700021
Showing with 21 additions and 14 deletions.
  1. +21 −14 webpack_in/entry.jsx
@@ -78,14 +78,27 @@ class ListWidget extends React.Component {
constructor(props) {
super(props);

this.state = {
this._mutateStateToAddItem = (state, strItemNew) => ({
...state,
items: [
"Item 1",
"Item 2",
"Item 3"
...state.items,
strItemNew
],
total_added: 3
};
total_added: state.total_added + 1
});

const objStateEmpty = {
items: [],
total_added: 0
};

this.state = this._mutateStateToAddItem(
this._mutateStateToAddItem(
this._mutateStateToAddItem(
objStateEmpty,
"Item 1"),
"Item 2"),
"Item 3");
}

render() {
@@ -108,14 +121,8 @@ class ListWidget extends React.Component {
return;
}

this.setState({
...this.state,
items: [
...this.state.items,
strItemNew
],
total_added: this.state.total_added + 1
});
this.setState(this._mutateStateToAddItem(this.state,
strItemNew));
}}/>
<ButtonWidget caption="Edit item..." />
<ButtonWidget caption="Remove item..." />

0 comments on commit 5bd1dc3

Please sign in to comment.