← Back to React Course | Chapter 3: Components & Props | Lesson 5 of 11

Default Props

Default props are the backup answer a component gives when nobody tells it what value to use.

Setting a Default with JS Default Parameters

Function components can use standard JavaScript default parameter syntax to provide a fallback value for a prop. If the caller doesn't pass that prop, the default is used automatically -- no extra configuration needed.

Note: Destructure props with defaults directly in the function signature for the cleanest syntax: function Greeting({ name = "Guest" }).

Warning: Defaults only kick in for undefined values -- passing null explicitly will NOT trigger the default.

Example: Setting a Default with JS Default Parameters

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Greeting({ name = "Guest" }) {
      return <p>Hello, {name}!</p>;
    }
    function App() {
      return <div><Greeting name="Alex" /><Greeting /></div>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  </script>
</body>
</html>

Why Default Props Matter

Providing sensible defaults means a component behaves reasonably even when used quickly without every prop specified. This reduces bugs and makes a component more pleasant to reuse across a codebase, since callers don't have to remember every optional setting.

Note: Give defaults to props that have an obvious normal value, like a button's type defaulting to "button".

Warning: Overusing defaults for props that should really be required can hide bugs where a value was forgotten by mistake.

Example: Why Default Props Matter

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Button({ label = "Click me", type = "button" }) {
      return <button type={type}>{label}</button>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<Button />);
  </script>
</body>
</html>

Defaults for Multiple Props

You can give default values to as many destructured props as needed in the same function signature. Each prop's default is independent -- a caller can override just one of them while still relying on defaults for the rest.

Note: Order doesn't matter when destructuring with defaults -- you can mix required and defaulted props freely.

Warning: A default value referencing another prop's current value isn't supported directly in destructuring -- compute derived values inside the function body instead.

Example: Defaults for Multiple Props

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
    function Avatar({ size = 40, alt = "User avatar" }) {
      return <div style={{width: size, height: size, background: "#ccc", borderRadius: "50%"}} title={alt} />;
    }
    function App() {
      return <div><Avatar /><Avatar size={80} /></div>;
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  </script>
</body>
</html>
Common Mistakes
  1. Assuming default parameter values only work with class components -- they actually work great with function components too.
  2. Setting a default value but still passing undefined explicitly, which still triggers the default (this is expected, but often surprising).
  3. Forgetting that passing null (not undefined) does NOT trigger the default value.
Chapter Summary
  • Default values ensure a component has sensible behavior even if a prop isn't passed.
  • In function components, JavaScript's default parameter syntax handles this directly.
  • A default only applies when the prop is undefined, not when it's null or an empty string.
  • Defaults make components more robust and easier to use.
Browser Support

No browser-specific restrictions -- uses standard JavaScript default parameters, available since ES6.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.