@@ -215,13 +215,10 @@ <h2>Add New Row</h2>
215
215
productInput.value = ''
216
216
priceInput.value = ''
217
217
quantityInput.value = ''
218
- // Get the component state:
219
- const state = this.state
220
218
// Check if user entered a product name:
221
219
if (product) {
222
220
// 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}))
225
222
} else {
226
223
// User hit Add button without entering product:
227
224
alert('Please provide a product name before trying to add a row.')
@@ -275,19 +272,15 @@ <h2>Updating Price and Quantity</h2>
275
272
// With that we can update the state.
276
273
const index = e.target.dataset.index
277
274
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)
281
276
}
282
277
283
278
updatePrice(e) {
284
279
// Get the array index stored on the input.
285
280
// With that we can update the state.
286
281
const index = e.target.dataset.index
287
282
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)
291
284
}</ code >
292
285
</ pre >
293
286
< 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>
317
310
< code class ="language-javascript ">
318
311
deleteRow(e) {
319
312
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))
324
314
}
325
315
</ code >
326
316
</ pre >
0 commit comments