liveBook · Manning

Default value

As explained in React in Depth

A default property value is the value assigned to a component property when the caller does not provide one.

A default value supplies a component property when the caller omits it. In functional components, destructuring provides the direct modern pattern; class-based components use the component’s defaultProps object. section 4.3.3

Choose the fallback according to the component shape and maintenance context:

  • Set a fallback during parameter destructuring, such as target="_self" or name="Stranger". section 4.3.3
  • Assign defaultProps on class-based components, where the pattern remains relevant for older codebases. section 4.3.3
  • Avoid defining the same fallback in both places: defaultProps overrides the matching destructuring default. section 4.3.3
  • Keep optionality separate from fallback choice: PropTypes.isRequired marks required properties, while optional properties do not necessarily need defaults. section 4.3.1

A link with a safe target

MenuItem can render a link with target="_self" when callers omit target. A functional component expresses this in its parameter list; a class component expresses the same behavior through MenuItem.defaultProps. section 4.3.3

function MenuItem({ label, href, target="_self" }) {    #1
  return (
    <li>
      <a href={href} title={label} target={target}>
        {label}
      </a>
    </li>
  );
}
MenuItem.propTypes = {    #2
  label: PropTypes.string.isRequired,
  href: PropTypes.string.isRequired,
  target: PropTypes.string,    #3
};

Inspect the functional MenuItem implementation, including its destructuring fallback and optional target property. section 4.3.3

class MenuItem extends React.Component {
  render() {
    return (
      <li>
        <a
          href={this.props.href}
          title={this.props.label}
          target={this.props.target}     #1
        >
          {this.props.label}
        </a>
      </li>
    );
  }
}
MenuItem.propTypes = {
  label: PropTypes.string.isRequired,
  href: PropTypes.string.isRequired,
  target: PropTypes.string,     #2
};
MenuItem.defaultProps = {    #3
  target: "_self",
};

Compare the class-based equivalent, where defaultProps supplies the target fallback. section 4.3.3

How React in Depth covers it

How deep it goes
Explains destructuring and defaultProps with precedence rules, but does not descend into runtime implementation internals.
Examples
Uses MenuItem target, Greeting name, and Car hasSunroof to illustrate fallback values and optional properties.
What it assumes
Assumes React fundamentals and distinguishes optional properties from required properties through PropTypes.
What you can do after
Apply it via destructuring or defaultProps, while avoiding duplicate fallback declarations and separating defaults from requiredness.
How it connects
Connects default values to React props, PropTypes, optional properties, required properties, and class-based components.

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 in Depth. The section links point to the parts of the book it draws on.