itenium-be / React-New-Features

React.js tutorial. Claim an exercise by replying to the issue. Create a PR per Task (per checkbox).
4 stars 4 forks source link

SockInventory: Fix bug that inventory update is not immediately visible #6

Open Laoujin opened 5 years ago

Laoujin commented 5 years ago

File: SockInventory.tsx

When clicking the "Add to cart" button on a Sock detail page, nothing seems to be happening. We need to go back to the prev screen and return before a rerender happens.

This is because the sock prop is mutated.

<button onClick={() => sock.inventory--}>
       Add to cart
</button>

Fix the bug by using state properly.

Create a PR per task