React · Chapter 5 of 26

Events

By the end of this chapter you can make a component respond to clicks and form submits with onClick and onSubmit, give React a function to call later rather than calling it yourself, use the event object (e.target, e.currentTarget, e.preventDefault()), tell a parent what happened through a function it passed down, stop a click from reaching the elements around it, and read a form's fields with FormData. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Thinking in components
  • 4 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

Everything Parda has built so far only shows things. A booking page is mostly things you press: a seat in the hall, the search box, a show in the list, Book now. On the opening night of Diwali week, 385 of Kala Talkies' 412 seats for Ghar Wapsi Express are already booked; in row B, nine of the eighteen seats are taken, and a taken seat must not respond at all.

With the DOM by hand you would find each button and call addEventListener on it, then remember to do it again for every button you add later, and to remove the listeners of every button you take away. In React, a handler is a prop on the element, written once in the JSX the component returns, like className. This chapter is still without state (that is chapter 7): a click calls a function, and the function tells someone. App's functions print to the Preview's console, so that is where you watch your clicks land.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    SeatButton.jsx: a seat you can pick, unless it is taken

    SeatButton gets three props: seat (the seat's id, such as "B8"), taken (true when the seat is booked) and onPick (a function from the parent). Make it return a <button> whose text is the seat, B8. Clicking the button calls onPick with the seat's id, onPick("B8"), once a click; not with the click's event object, and never while the component renders. When taken is true, the button is disabled, so a click calls nothing. Give the button className="seat", or "seat taken" when it is taken, for app.css; the Check does not look at classes.

    The Check clicks B8 (free) and expects one call with "B8", then clicks B11 (taken) and expects no call and a disabled button.

  2. Task 2

    SearchForm.jsx: a search that tells its parent

    SearchForm gets one prop, onSearch, a function from the parent. Make it return a <form>, with className="search" for app.css, that holds in this order: a <label> that says Search shows, with htmlFor="show-search"; an <input> with type search, the id show-search, the name q and the placeholder Film, play or artist; and a <button> that says Search.

    When the form is submitted, by clicking Search or by pressing Enter in the box, call onSearch once with the text in the box without the spaces at its ends: typing " pune 52 " and clicking Search calls onSearch("pune 52"). new FormData(form) reads a form's fields by their names. The form must not let the page reload: the Check fails if the submit event is not stopped, for a click and for Enter.

  3. Task 3

    ShowRow.jsx: a row that opens, a button that books

    ShowRow gets three props: show (an object with an id, a title, a venue and a time) and two functions from the parent, onOpen and onBook. Make it return an <article>, with className="show-row" for app.css, that holds a heading h3 with the title, a paragraph with the venue and the time (Kala Talkies, 9:30 pm), and a <button> that says Book.

    Clicking anywhere on the row, its title for example, calls onOpen with the show's id, onOpen("T0023"), once. Clicking Book calls onBook with the show's id, onBook("T0023"), once, and must not call onOpen: the Book button is inside the row, so its click reaches the row too unless Book's handler stops it. Neither is called while the component renders.

  4. Task 4 · interview question

    Interview: BookButton.jsx, booked before anyone clicked

    BookButton gets a showtime's id in its showtimeId prop and a function, onBook. It shows a <button> that says Book now, with className="book" for app.css, and clicking it must call onBook with the id, onBook("T0023"), once a click. Datta's log shows a booking for T0023 every time the page opens, and nothing when someone clicks: the Preview's console says Booking T0023 before anything is clicked. Fix it, so that onBook is called only when the button is clicked. The Check renders it with T0023 and with T0038, checks that nothing is called while it renders, then clicks.

    Rukhsana will ask: what is the difference between onClick={onBook} and onClick={onBook(id)}, and when does each one run?

What you will learn

  • onClick, onSubmit, onChange: props that take a function React calls when the event happens.
  • Pass a function, do not call it: onClick={handleClick}, never onClick={handleClick()}.
  • The event object: e.target, e.currentTarget and e.preventDefault().
  • Events up: a child calls a function from its props (onPick), the parent decides what happens.
  • Bubbling and e.stopPropagation(): a click reaches every element around the one clicked.
  • FormData: read a form's fields when it is submitted.

More in Thinking in components

Build "Events" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React are free. This one comes with the all labs plan.