Assignments > Tutorial 9: React: Custom & Third-Party Components (Ant Design)
Due Mon, 03/30 at 11:59pm
Introduction
By the end of this tutorial, you should be able to do the following:
Explain why components matter. Describe how breaking the UI into reusable pieces (with props for different data) reduces duplication, keeps layout consistent, and makes it easier to reason about one small part of the screen at a time.
Compare “build it yourself” vs. “use a design system.” Give sensible pros and cons. For example:
- Custom components offer full control and no extra dependencies, but cost more time and accessibility polish
- Library components (such as Ant Design) speed you up and bundle patterns like spacing and keyboard support, but tie you to their API, bundle size, and update schedule.
Choose a strategy for a simple screen. For a given widget (card, gallery, form control), argue when you would start from scratch, when you would reach for a third-party component, and what you would trade off either way.
1. Repeat Wednesday’s React Setup
Open React Activity and complete Steps 2-4 again, but do the coding work in a folder named tutorial09.
Use this folder location:
csci344/tutorials/tutorial09
By the time you finish this part, you should have:
index.html filesrc folderApp.jsx filemain.jsx filenpm run dev2. Build a Card component from scratch
Build a custom React component (no Ant Design yet) called Card.
It must accept at least three props:
name— text title for the cardimage_url— URL string for the imagedescription— short text body
What to do
-
Add a
componentsfolder undersrcif you do not have one yet. -
Create
Card.jsxandCard.cssin thecomponentsdirectory. -
In
Card.jsx, render at least: an<img>(useimage_urlandnameforalt), a heading forname, and a paragraph (or similar) fordescription.import React from "react"; import "./Card.css"; export default function Card({ name, image_url, description }) { return ( <section className="card"> <h2>{name}</h2> {/* add remaining HTML tags + data */} </section> ); } -
Style it in
Card.cssso it clearly looks like a card (border or shadow, spacing, readable type). -
In
App.jsx, importCardand render at least two cards with different prop values, for example:<Card name="Sample item" image_url="https://picsum.photos/id/237/400/300" description="A short description goes here." />You can also put a few objects in an array and use
.map(...); if you do, give each list item a stablekey.
3. Install Ant Design (antd)
-
Stop the dev server (Ctrl+C in the terminal).
-
Install Ant Design:
npm install antd -
Load Ant Design’s styles once in your entry file (usually
main.jsx), above your other imports that render UI:import "antd/dist/reset.css"; -
Open the Ant Design docs for the Image component:
ant.design/components/image -
On that page, find a basic example (often the first demo). Copy the import line for
Imagefrom the example (or from the code snippet the docs show). -
Paste that import at the top of
App.jsx, with your other imports (for example belowimport React ...and above yourCardimport). -
Copy the JSX for the
Imagewidget from the same example (the part that looks like<Image ... />or a small group of images). -
Paste that JSX inside the
return (...)of yourAppcomponent (usually inside<main>or a wrapper) so it appears on the page when you run the app. -
Adjust
srcorwidthif you want your own photo URLs. Then, save and runnpm run devand confirm the Ant Design image renders.
This step is practice reading official docs, placing imports in the right file, and placing components in valid JSX.
4. Create AntCard.jsx
Add a second component, AntCard.jsx, that shows the same three pieces of information as your custom Card, but built with Ant Design’s Card component.
- Create
src/components/AntCard.jsx. - Import
Cardfromantd(and anything else you need fromantdfor layout). - Give
AntCardthe same props as your custom card:name,image_url,description. - Map them into Ant Design’s API in a sensible way, for example:
- use Ant Design’s
coverprop (orCard’s body) for the image - use
title={name}or a heading inside the card for the name - put
descriptionin the card body
- use Ant Design’s
- In
App.jsx, render at least one<AntCard ... />next to (or below) your custom<Card />so you can compare the two.
What to Think About
When you compare your custom
Cardand yourAntCard, ask yourself:
- Speed: Which one was faster to build?
- Control: Which one gave you more freedom to style exactly what you wanted?
- Complexity: Which one required more code and more decisions?
- Consistency: Which one made it easier to keep spacing, typography, and layout consistent?
5. Try at least two more antd components
Beyond the Image from section 3 and the Card inside AntCard.jsx, add at least two other distinct antd components somewhere in your app (for example a Button, Tag, Space, Typography, Carousel, Drawer, etc.).
Skim the Ant Design components list and wire up anything that fits your page.
Requirement: Your finished app should use at least four different antd component types: Image (section 3), Card inside AntCard.jsx (section 4), and two others you add in this section (for example Button and Tag).
| Idea | Link |
|---|---|
| Carousel | Carousel |
| Time Picker | Time Picker |
| Drawer | Drawer |
| Calendar | Calendar |
| Collapse | Collapse |
| Tour | Tour |
What to Submit (Please Read Carefully)
First, ensure that you have completed the tutorial requirements:
Card component that uses name, image_url, and description props.<Card /> components with different data.antd and added import "antd/dist/reset.css"; in main.jsx.Image example and rendered it in App.jsx.AntCard.jsx using Ant Design's Card component with the same three props.<AntCard /> in App.jsx.antd component types (beyond Image and Card).tutorial09/dist/index.html (see below)Remember that your Browser doesn’t understand React because it uses some language features that aren’t HTML, CSS, or JavaScript. We therefore need to “transpile” our code so that it can be displayed on our GitHub server. To do this:
Bundling Instructions
-
Create a new script entry in your
package.jsonfile to teach vite how to publish your react. It should look something like this;{ ... "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "vite dev", "build":"vite build" <-- NEW ENTRY HERE }, ... } -
Create a new file in the root of your
tutorial09folder calledvite.config.js -
Paste in the following code:
import { defineConfig } from "vite"; export default defineConfig({ base: "", build: { rollupOptions: { output: { entryFileNames: "main.js", assetFileNames: "main.css", chunkFileNames: "chunk.js", manualChunks: undefined, }, }, }, }); -
Navigate to your
tutorial09folder on the command line and run the following:
npm run build.- This should transpile your code in a folder called
dist.
- This should transpile your code in a folder called
-
Next, make sure you link to the compiled version of your code (
tutorial09/dist/index.html) from your homepage.
To submit this tutorial, commit and push all of your edits to GitHub and, paste a link to your GitHub Repository and to your GitHub pages in the Moodle submission for Tutorial 9.