Lab 9: Responding to an Event
Objectives
- Add a button
- Handle the click event
Steps
Add a button
Open the file
src\projects\ProjectCard.tsx
Add an edit button to the
ProjectCard
using theHTML
snippet below.<!-- place this below the <p>Budget: ...</p> --><button class="bordered"><span class="icon-edit "></span>Edit</button>Remember you will need to change some things about the
HTML
to make it validJSX
src\projects\ProjectCard.tsx
...<p>Budget...</p><button className=" bordered"><span className="icon-edit "></span>Edit</button>Verify the button displays in your browser.
Handle the click event
Add a
handleEditClick
event handler toProjectCard
that takes aproject
as an argument and logs it to theconsole
.src\projects\ProjectCard.tsx
function ProjectCard(props: ProjectCardProps) {const { project } = props;+ const handleEditClick = (projectBeingEdited: Project) => {+ console.log(projectBeingEdited);+ };return (<div className="card"><img src={project.imageUrl} alt={project.name} /><section className="section dark"><h5 className="strong"><strong>{project.name}</strong></h5><p>{project.description}</p><p>Budget : {project.budget.toLocaleString()}</p><button className=" bordered"><span className="icon-edit "></span>Edit</button></section></div>);}Wire up the click event of the edit button to the
handleEditClick
event handler.src\projects\ProjectCard.tsx
function ProjectCard(props: ProjectCardProps) {const { project } = props;const handleEditClick = (projectBeingEdited: Project) => {console.log(projectBeingEdited);};return (<div className="card"><img src={project.imageUrl} alt={project.name} /><section className="section dark"><h5 className="strong"><strong>{project.name}</strong></h5><p>{project.description}</p><p>Budget : {project.budget.toLocaleString()}</p><buttonclassName=" bordered"+ onClick={() => {+ handleEditClick(project);+ }}><span className="icon-edit "></span>Edit</button></section></div>);}
3) Verify the application is working by following these steps:
Open the application in your browser and refresh the page.
Open the Chrome DevTools to the
console
(F12
orfn+F12
on laptop).Click the edit button.
Verify the project is logged to the Chrome DevTools
console
.