Search⌘ K
AI Features

Specifying a default for a function component prop

Explore how to implement default values for function component props in React with TypeScript. Understand the differences between using defaultProps and default function parameters approaches. Gain knowledge to create strongly-typed props that improve code readability and maintainability.

In the CodeSandbox project we were working on in the last lesson, we are going to implement a default value of “How are you?” for the message prop. We are going to implement this in 2 different ways.

defaultProps approach

The first way is to use a static property called defaultProps on the function component. This is set to an object literal that contains the values to set the props to:

Component.defaultProps = {
  prop1: value1,
  prop2: value2,
  ...
}

In our CodeSandbox project, use the ...