Lab 6: Passing Data to a Component
Objectives
- Create a reusable list component
- Pass data into a component property
Steps
Create a reusable list component
Create the file
src\projects\ProjectList.tsxImplement a
ProjectListfunction component that meets the following specifications:- Takes a
projectsarray as aprop.You will need to create an interface to define the properties that come into the component.
- Displays the
projectsarray as aJSON string.
src\projects\ProjectList.tsximport React from 'react';import { Project } from './Project';interface ProjectListProps {projects: Project[];}function ProjectList({ projects }: ProjectListProps) {return <pre>{JSON.stringify(projects, null, ' ')}</pre>;}export default ProjectList;- Takes a
Pass data into a component property
Modify
src\projects\ProjectsPage.tsxto render theProjectListcomponent and pass it theMOCK_PROJECTSarray instead of directly displaying the data.src\projects\ProjectsPage.tsximport React from 'react';import { MOCK_PROJECTS } from './MockProjects';+ import ProjectList from './ProjectList';function ProjectsPage() {return (<><h1>Projects</h1>- <pre>{JSON.stringify(MOCK_PROJECTS, null, ' ')}</pre>+ <ProjectList projects={MOCK_PROJECTS} /></>);}export default ProjectsPage;Verify the application is displaying the projects as it was in the last lab.
