React · Chapter 26 of 26

The interview

By the end of this chapter you can answer five questions that frontend interviews ask about React, with working code: debounce a value, build tabs that a keyboard and a screen reader can use, load pages of results that never mix up, write a component that works both controlled and uncontrolled, and find three classic bugs in one component by asking what React saw. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real apps
  • 5 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

The site goes live on Friday 6 November, and Parda is hiring a second frontend engineer to look after it. Sayali asked Rukhsana to write the interview, and Rukhsana did what she always does: she took five things that went wrong or nearly went wrong while the site was being built, and turned each into a question. Before she asks a candidate, she asks you.

Each question is small enough to write in a few minutes, and each one hides a trap that a quick answer falls into. Nothing in this chapter is new React: every hook it needs has been taught. What is new is reading a problem, choosing among the tools, and saying why, out loud, as an interviewer wants to hear it.

What you build: 5 tasks

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

  1. Task 1 · interview question

    Interview: DebouncedSearch.jsx, a search that waits for typing to stop

    Write useDebounce(value, ms), exported by name: a hook that returns value, but only once value has stayed the same for ms milliseconds; until then it returns the last value that did (the first value at once). Then make DebouncedSearch, the default export, show a search box (an input with type search) labelled Search shows. When the typing has stopped for 300 ms, it calls searchShows(text) from api.js once, with the text as it is then; while that request runs it shows a paragraph Searching...; then a list with one item per show, each the show's title. No request while the box is empty or only spaces, and only the answer for the latest text is shown, even when an older answer arrives later.

    The Check uses fake timers. It tries useDebounce(value, 500) on its own, then types p, u, n, e 100 ms apart and expects no request before 300 ms after the last key and one, with "pune", at 300 ms; then it clears the box, types ka, waits, types a, and answers kaa before ka.

    Rukhsana will ask: what is debouncing, and why is it a hook of a value here rather than a function that wraps the request?

  2. Task 2 · interview question

    Interview: Tabs.jsx, tabs that everyone can use

    Make Tabs, with the prop tabs (an array of { id, label, content }), show an element with role="tablist" labelled Show details (aria-label), holding one tab per item: a button with role="tab" that says its label, with aria-selected "true" on the chosen tab and "false" on the others, and tabIndex 0 on the chosen tab and -1 on the others. Under it, one element with role="tabpanel" shows the chosen tab's content; the panel's aria-labelledby is the chosen tab's id, and each tab's aria-controls is the panel's id.

    The first tab is chosen at the start, and a click chooses a tab. With the focus on a tab, ArrowRight and ArrowLeft choose the next and the previous tab and move the focus to it, going round at the ends; Home and End choose and focus the first and the last. Every id must be unique, even with two Tabs on one page: build them from useId. The Check puts two Tabs on a page, then uses one with About, Cast and Reviews, clicking and pressing the keys.

    Rukhsana will ask: what makes a set of tabs accessible, and what does a screen reader announce for each part?

  3. Task 3 · interview question

    Interview: LoadMore.jsx, Load more, never mixed up

    Make LoadMore show a select labelled Kind with the options all, film, play, comedy and music (each value the kind), all at first; a list with one item per show loaded so far (its title); a button Load more while there are more, which says Loading... and is disabled while a page loads; and, when there are no more, a paragraph That's every show.

    It calls getShowsPage({ kind, page }) from api.js for page 1 at the start and for the next page on each Load more; the answer is { shows, hasMore }, and its shows go at the end of the list. Changing the kind starts again from page 1 of that kind with an empty list. Never mix up answers: a page that arrives for a list that has since been started again must be ignored. The Check answers when it wants to: it clicks Load more twice fast (page 2 must be asked for once), changes Kind to comedy while page 2 of all is on its way, answers comedy's page 1, then all's page 2.

    Rukhsana will ask: how do you make sure a slow answer never lands in the wrong list?

  4. Task 4 · interview question

    Interview: Rating.jsx, a rating, controlled or not

    Make Rating, with the props label, value, defaultValue and onChange, show a <fieldset> whose <legend> is the label, holding five radio inputs labelled 1 star, 2 stars, 3 stars, 4 stars and 5 stars. Controlled, when value is given: the checked one is value; a click calls onChange with the star's number and changes nothing until the parent passes a new value. Uncontrolled, when value is missing: it keeps its own rating, starting at defaultValue (nothing checked if that is missing too); a click checks that star and calls onChange with its number, if onChange is given.

    No warnings either way. The Check uses defaultValue={3} with an onChange, a Rating with only a label, and value={2} with an onChange (then value={4}).

    Rukhsana will ask: what is the difference between a controlled and an uncontrolled component, and when would you build one that can be both?

  5. Task 5 · interview question

    Interview: Watchlist.jsx, three bugs in one component

    Watchlist, with the props shows (the list at the start) and extra (a show to add), has three bugs: a stale value in a timer, a key that is not the show's, and an array changed in place. Fix all three, so that it shows a heading h2 My watchlist; a paragraph Open for 0 s that counts up once a second (Open for 3 s after 3 seconds); a list with one item per show, each with its title, an input labelled Note for <title> (Note for Pune 52) and a button Remove <title>; and a button Add <extra's title> that adds the extra show at the end and is then gone. A note typed for a show stays with that show when another show is removed.

    The Check uses fake timers, then adds Raag Diwali, types a note for The Tiffin Thief and removes Pune 52.

    Rukhsana will ask: for each bug, what did React see, and why did the page not show what the code meant?

What you will learn

  • Interview: DebouncedSearch.jsx, a search that waits for typing to stop
  • Interview: Tabs.jsx, tabs that everyone can use
  • Interview: LoadMore.jsx, Load more, never mixed up
  • Interview: Rating.jsx, a rating, controlled or not
  • Interview: Watchlist.jsx, three bugs in one component

More in Real apps

Build The interview 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.