liveBook · Manning

Component

As explained in React Quickly, Second Edition

A functional component is a React component defined as a JavaScript function that receives properties as its argument and returns user-interface output.

Need the same kind of link in several places? Make it a component. A React component is a reusable piece of a user interface; each use can receive different properties, or props. Components can contain HTML elements and other components, so a small link can become part of a menu or a larger application. section 1.0 section 4.1.1

Make a link component

function MenuItem({ href, label }) { return <li><a href={href}>{label}</a></li>; } function Menu() { return <ul><MenuItem href="/about/" label="About" /></ul>; } MenuItem is a functional component: a JavaScript function that returns UI output. Menu uses its capitalized name as a JSX tag and supplies href and label as props. The function destructures those values from its props argument, then uses them in the returned JSX. A second MenuItem can reuse the same definition with different values. section 4.1.1 section 4.1.2

Destructuring is optional. You can write function MenuItem(props) and read props.href and props.label instead. JSX is also optional: React.createElement can create the same kinds of elements, though JSX makes nested components easier to read. Pass child content between a component’s tags; it arrives through the specially named children prop. section 4.1.1 section 4.1.2 section 2.9

Listing 4.2 Menu application using functions
import "./App.css";
function App() {                   #1
  return (
    <main>
      <Menu />
    </main>
  );
}
function Menu() {                  #1
  return (
    <nav className="navbar">
      <h1 className="title">TheMenuCompany</h1>
      <ul className="menu">
        <MenuItem label="Home" href="/" />
        <MenuItem label="About" href="/about/" />
        <MenuItem label="Blog" href="/blog" />
      </ul>
    </nav>
  );
}
function MenuItem(props) {         #2
  return (
    <li className="menu-item">
      <a className="menu-link" href={props.href}>
        {props.label}
      </a>
    </li>
  );
}
export default App;

Follow the complete menu from App through Menu to three MenuItem instances, each supplied with its own label and destination. section 4.1.1

A component can also be defined as a class with a render method. For a simple component, replacing that method with a function and replacing this.props with a props argument gives a shorter definition. Functional components are the usual choice, but classes remain supported. An error boundary is one reason to use a class; an established class-based codebase may be another. Hooks brought functional components capabilities once associated with classes. section 4.4.1 section 4.0 section 4.3

How React Quickly, Second Edition covers it

How deep it goes
Works through functional definitions, prop destructuring, and class-to-function conversion; leaves interacting gallery components for later treatment.
Examples
Uses a nested website menu and an image gallery to compare component definitions and property handling.
What it assumes
Assumes basic JavaScript functions while introducing React elements, components, props, and destructuring through examples.
What you can do after
Build it from scratch as a functional component, pass props, nest instances, and convert a simple render-only class.
How it connects
Connects components to JSX, props, component composition, class components, and hooks.

Also explained in

Compare how 10 books explain component →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.