Exercises
Art Store
In the sandbox below, we're building an e-commerce store for an artist.
The header has a cute shopping cart button, and the button has a badge that increments as the user starts adding items to their cart:
The components are structured in the following hierarchy:
App
Header
CartButton
Using what we learned in the previous lesson, restructure things so that App
owns the CartButton
component, without changing the DOM structure at all.
Acceptance Criteria:
- The
<CartButton>
element should be owned by theApp
component, rather than<Header>
. - The DOM structure should not be affected (the cart button should still be a child of the
<header>
DOM node). - Bonus: Using what we learned in the “Leveraging Keys” lesson, update the code so that the “fade” animation retriggers whenever the number changes:
Code Playground
Solution:
Note: In this video, I share my solution, but I also talk about whether this solution is actually a good idea or not. Even if you solved the exercise without issue, I'd suggest checking out the second half of this video.
More info
This “lifting content up” concept has been covered extensively online, largely as a way to potentially improve performance. If you'd like to go deeper, I recommend the following resources:
- “Before You Context”, from the React docs
- “Before you Memo”, by Dan Abramov
- “One Simple Trick to Optimize Re-Renders”, by Kent C. Dodds
Please consider these as optional resources, if you want to go deeper into this stuff. It's absolutely not required!