As a starting point, the CartContext with initial state has been copied into the 04-complete-react-state directory.
Let's explore how to manage user-generated reviews and dynamically update average ratings in our app.
Current State of Reviews in the Application
When visiting a product page, you'll see a set of reviews. You also have the option of adding your own review by typing in some text and setting the rating.
When submitting a new review, two updates occur:
- The review is added to the list of existing reviews.
- The average rating will be updated and displayed above the "Add to Cart" button.
Examining the Code Structure
The code for the product pages is in an RSC located at app/products/[id]/page.tsx.
Here, the ProductDetail component takes the product id which is then used to fetch the corresponding product details.
For reference, let's look at the Product information.
The Product & Review Types
A Product is an interesting mix of both mutable and immutable data.
At src/api/types.ts, we can see the definition:
All of the properties except for the reviews are immutable.
Each Review consists of a rating and text:
The ProductDetail React Server Component will show the immutable data, and the mutable reviews data will be managed with client components.
Utilizing Client Components
Reviews are handled in the Reviews and AverageRatings client components.
The Reviews component allows you to send a review by typing text that will be stored as local state and then sent to the server via the addReviewAction:
The addReviewAction is defined in the parent page RSC and then passed down into the Reviews client component.
The AverageRatings component takes in reviews and calculates the current average rating.
Challenge
For this exercise, your goal is to wire everything together by creating components for a ReviewsContext and ReviewsProvider that will send the reviews to the AverageRating and Reviews components.
Note that every time the customer navigates between routes, the products/[id]/page.sx component will be re-created.
As a hint, the output of the addReviewAction is the updated array of reviews.