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: - Prompt for the
CommentListcomponent: - Prompt for the
NewCommentFormcomponent:
"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."
"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."
"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:
- After Codex generates the code and you review it, open your terminal and run:
"Add a new Drizzle table calledbookmarkswith anid, auserIdforeign key to users, apostIdforeign key to posts, and acreatedAttimestamp."
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: If a teammate already did this exercise before you, you might see a message saying the table already exists or that there's nothing to change. That's totally fine! It just means the
bookmarks table is already in your shared database. You still practiced the important part: writing the schema and running the push.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!