liveBook · Manning

Default value

As explained in React Quickly, Second Edition

A default property value is a value assigned when an object property is absent or unspecified during destructuring.

A default value supplies a component property when callers leave it unspecified. In React components, this is ordinary JavaScript syntax used with destructured properties, not React-specific behavior. section 4.1 section 4.1.3

Defaulting a menu link target

function MenuItem({ label, href, target = "_self" }) { return ( <li className="menu-item"> <a className="menu-link" href={href} target={target}> {label} </a> </li> ); } <MenuItem label="Home" href="/" /> <MenuItem label="Blog" href="/blog" target="_blank" /> Home uses "_self" automatically, while Blog overrides the default with "_blank". Properties with defaults are commonly placed after nondefault properties, although either order is valid. section 4.1.3

Listing 4.6 Menu items with a default target
...
function Menu() {
  return (
    <nav className="navbar">
      <h1 className="title">TheMenuCompany</h1>
      <ul className="menu">
        <MenuItem label="Home" href="/" />               #1
        <MenuItem label="About" href="/about/" />        #1
        <MenuItem
          label="Blog" href="/blog" target="_blank"      #2
        />
      </ul>
    </nav>
  );
}
function MenuItem({ label, href, target = "_self" }) {   #3
  return (
    <li className="menu-item">
      <a
        className="menu-link"
        href={href}
        target={target}
      >
        {label}
      </a>
    </li>
  );
}
...

Compare explicit targets on every menu item with a concise component that defaults target to "_self" and overrides it only for the blog link. section 4.1.3

Related uses distinguish several kinds of default values:

  • Use defaultValue for the initial value of an uncontrolled form input; do not also set its value property. section 9.6
  • Pass a default value to createContext; useContext returns it when no associated provider is found. section 10.1.3 section 10.5

How React Quickly, Second Edition covers it

How deep it goes
Defines syntax, ordering practice, and UI examples, but stops short of language internals.
Examples
Demonstrates menu link targets, uncontrolled input initialization, and context fallback values.
What it assumes
Assumes basic JavaScript function parameters and teaches the React usage through destructuring examples.
What you can do after
Apply it via destructured component parameters, uncontrolled inputs, and createContext fallbacks.
How it connects
Connects default values with destructuring, React components, uncontrolled inputs, and React Context.

Also explained in

Compare how 20 books explain default value →

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.