How to learn react logo

These tutorials will give you a deeper understanding of React.js. Learn with examples from the Javascript UI library, covering the basics and the advanced.

Browse by tag:

Specifying default props in functional and class components

Props

In React we can define the default properties and we can do it in only a few lines of code. Default props come in handy if we need to make sure that a component's props appear a certain way, regardless of whether a parent component passed anything down to them.

You can specify default props by following this syntax:

Component.defaultProps = { propName: 'Default value' }

The defaultProps property should be set to an object representing the default props for the component, outside of the class body, as shown in the following code snippet:

class Phone extends React.Component {
  render() {
    // ...
  }
}

// Set default props
Phone.defaultProps = {
  numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  power: "on"
}

As with class components, you can set default props on a functional component (by adding a static property named defaultProps):

function Phone = (props) => {
  render() {
    // ...
  }
}

// Set default props
ThemedButton.defaultProps = {
  numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  power: "on"
}

For one more concrete example, let's say you want to greet somebody, but you don't yet know their name. I got this from the React Doc for defaultProps:

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

// Specifies the default values for props:
Greeting.defaultProps = {
  name: 'Stranger'
};

Now, on the screen, we will see the phrase Hello, Stranger. Without defaultProps we would see a blank space.

How To Learn React is designed and developed by me, Blake Fletcher. It is a project intended to share the things I learn while working with the UI library. As I build things, complete tutorials, read blog posts, and watch videos, I'll add to the site. I hope to eventually bring on other contributors. If interested, send me an email at blkfltchr@gmail.com.