Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit b9350de

Browse files
Robert BiggsRobert Biggs
Robert Biggs
authored and
Robert Biggs
committed
update
1 parent 92a3730 commit b9350de

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

tuts/thinking-in-composi.html

+4-14
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,10 @@ <h2>Add New Row</h2>
215215
productInput.value = ''
216216
priceInput.value = ''
217217
quantityInput.value = ''
218-
// Get the component state:
219-
const state = this.state
220218
// Check if user entered a product name:
221219
if (product) {
222220
// Update component state with new product:
223-
state.push({product, price, quantity})
224-
this.state = state
221+
this.setState(prevState => prevState.push({product, price, quantity}))
225222
} else {
226223
// User hit Add button without entering product:
227224
alert('Please provide a product name before trying to add a row.')
@@ -275,19 +272,15 @@ <h2>Updating Price and Quantity</h2>
275272
// With that we can update the state.
276273
const index = e.target.dataset.index
277274
const value = Number(e.target.value)
278-
const state = this.state
279-
state[index].quantity = value
280-
this.state = state
275+
this.setState(prevState => prevState[index].quantity = value)
281276
}
282277

283278
updatePrice(e) {
284279
// Get the array index stored on the input.
285280
// With that we can update the state.
286281
const index = e.target.dataset.index
287282
const value = Number(e.target.value)
288-
const state = this.state
289-
state[index].price = value
290-
this.state = state
283+
this.setState(prevState => prevState[index].price = value)
291284
}</code>
292285
</pre>
293286
<p>Then we need to update the <code>componentDidMount</code> hook and the <code>handleUpdate</code> method:</p>
@@ -317,10 +310,7 @@ <h2>Deleting a Row</h2>
317310
<code class="language-javascript">
318311
deleteRow(e) {
319312
const index = e.target.dataset.index
320-
console.log(index)
321-
const state = this.state
322-
state.splice(index, 1)
323-
this.state = state
313+
this.setState(prevState => prevState.splice(index, 1))
324314
}
325315
</code>
326316
</pre>

0 commit comments

Comments
 (0)