React Component Lists
- Create and Run the Next.js Project
Create the project
npx create-next-app@latest components-lists-exampleGo in to the foldercd components-lists-exampleRun the projectnpm run dev - In the “index.js”
- remove everything in the “main” tag except for the title (“h1”).
- change it to a title you’re comfortable with.
- remove the footer.
- Observe the differences on the page as you make these changes.
- Create a new “components” folder and in that folder create a component file named “SimpsonsCharacters.js”. In that file create a function named “SimpsonsCharacters” like you would for any component (this should become second nature shortly.)
- import that file in your “index.js” file.
import SimpsonsCharacters from '../components/SimpsonsCharacters'- use the component in your JSX
<SimpsonsCharacters />- in the “SimpsonsCharacters.js” file add a list of characters that we’re going to loop over.
const SIMPSON_CHARACTERS = [ "Homer Simpson", "Bart Simpson", "Marge Simpson", "Mr. Burns", "Lisa Simpson", "Apu Nahasapeemapetilon", "Sideshow Bob", "Milhouse Van Houten", "Ned Flanders",]- In the “SimpsonsCharacters” we’re going to take a look at how to loop over lists in JSX.
- We’re going to loop over the
SIMPSON_CHARACTERSusingmapby using{}in the JSX to show that we’re going to be using “js” in your JSX. - Below we’re going to see that we use the
keyon each<li></li>element because this is a requirement with react. More on lists and keys here - The component now will look somethis like this.
// SIMPSON_CHARACTERS defined up here.
export default function SimpsonsCharacters() { return <ul> {SIMPSON_CHARACTERS.map((characterName, index)=> { return <li key={index}>{characterName}</li> })} </ul>}- Observe the differences on the page as you make these changes.