Welcome to Week 4! Youâve learned to read data and write data. Now itâs time to build more complex, interactive features. This week, weâll be working with features that involve multiple components, client-side state, and even changing the database schema. This is where things get really fun!
đ§± What is a multi-component feature?
So far, most of the features youâve built have lived in a single file. But as applications grow, we often need to break down features into smaller, reusable components. A feature like a comment section, for example, might be made up of:
- A
CommentListcomponent to display all the comments.
- A
Commentcomponent for a single comment.
- A
NewCommentFormcomponent to add a new comment.
This is a good practice because it makes your code easier to read, test, and reuse. Your AI partner can be a huge help in this process.
â Exercise 1: Prompting for multiple components
Let's say you're working on the "Add Comments to Posts" feature. You need to build the UI for displaying comments and adding a new one.
Instead of asking Codex to build the whole thing at once, you can ask it to build each component separately:
- Prompt for the
Commentcomponent:
"Create a new React component calledCommentthat takes a comment object (with an author and text) as a prop and displays the authorâs name and the comment text."
- Prompt for the
CommentListcomponent:
"Create a new React component calledCommentListthat takes an array of comment objects as a prop. It should map over the array and render aCommentcomponent for each one."
- Prompt for the
NewCommentFormcomponent:
"Create a new React component calledNewCommentFormthat has aTextareaand a âSubmitâ button."
Now you have three small, clean components that you can assemble on your post detail page. This is much better than having one giant, messy file!
đïž Extending the database schema
Sometimes a new feature requires storing a new kind of data. This means you have to add a new table to your database schema and push those changes to Neon.
â Exercise 2: Practice a schema change
Let's practice adding a new table before you do it for real.
- Open the file
db/schema.ts.
- Prompt Codex:
"Add a new Drizzle table calledbookmarkswith anid, auserIdforeign key to users, apostIdforeign key to posts, and acreatedAttimestamp."
- After Codex generates the code and you review it, open your terminal and run:
pnpm run db:push
How to self-validate: You should see a success message in your terminal saying the changes were applied! To be totally sure, ask Codex: "How do I verify this new table exists in my Neon database dashboard?"
Note on Optimistic UI: If your ticket involves buttons that change state (like an RSVP or Like button), you might want the UI to update instantly before the database finishes saving. This is called an optimistic update. Ask Codex: "Show me how to implement an optimistic UI update for a toggle button in React."
đ Using Auth Context to protect pages
Some pages (like a User Profile) should only be visible to people who are logged in. In our app, we handle this using a client-side React Auth Context.
â Exercise 3: Check if a user is logged in
- Open a client component in your app (any file with
"use client"at the top).
- Prompt Codex:
"In a Next.js client component, how do I use theuseAuthhook from our auth context to check if a user is logged in, and redirect them to the homepage if they're not?"
How to self-validate: Log out of the app in your browser, then try to navigate directly to the page you just protected (e.g.,
http://localhost:3000/profile). If it instantly kicks you back to the homepage, your protection works!đïž Your tickets for this week
This weekâs tickets are a big step up in complexity. Youâll be building:
- Ticket #7: Build full-stack comment system for posts: A feature that involves multiple components, a new API endpoint, and connecting them together.
- Ticket #8: Build a basic user profile page: This involves using the React auth context on the client side to check if a user is logged in, rather than server-side sessions.
- Ticket #9: Add ability for users to RSVP to events: This is the most complex ticket yet! It requires you to add a new table to the database (and run
pnpm run db:pushto sync it to Neon) and handle optimistic UI updates.
Donât be intimidated! The process is the same as before: break the problem down into small steps, and ask your AI partner for help at each step. This is where youâll really start to see the power of AI-assisted development.
This is a challenging but very rewarding week. By the end of it, youâll have built features that are at the core of any modern web application. Good luck!