question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default props in ES6 class syntax

See original GitHub issue

The ES6 support announcement says:

the idiomatic way to specify class state is to just use a simple instance property. Likewise getDefaultProps and propTypes are really just properties on the constructor.

This makes a lot of sense to me, but I noticed some small inconsistencies that may be worth rethinking.

When using the original .createClass syntax, the value returned by getDefaultProps seems to be used at other points in the component lifecycle – not just in the constructor. For example, if I inspect what gets sent to componentWillReceiveProps(props), I can see that the default props are applied.

This doesn’t seem to be the case when using the ES6 class syntax, which means I have to duplicate code. Here’s an example of what I mean:

class Control extends React.Component {

  constructor(props) {
    props.value = props.value || '';
    super(props);
  }

  // ...

  componentWillReceiveProps(props) {
    props.value = props.value || '';
    // Do something with props...
  }

}

As you can see, I’m duplicating the expression props.value = props.value || ''. If I had more than one default, I’d obviously have a lot more duplication.

When using the .createClass method, I could return {value: ''} from the getDefaultProps method, and this would work, and I’d only have to do it once.

Does it make sense to restore this method to avoid unnecessary duplication? Is there another, more React-like approach that I’m not aware of?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:40 (11 by maintainers)

github_iconTop GitHub Comments

121reactions
sophiebitscommented, Apr 22, 2015

That’s correct. Instead, you write this:

class Control extends React.Component {

  // ...

  componentWillReceiveProps(props) {
    // Do something with props...
  }

}
Control.defaultProps = {value: ''};

Does that work for you?

102reactions
ghostcommented, Jan 5, 2016

Because I landed here from a Google search, and I prefer the class to encapsulate my code, the default props can be set using computed properties like so:

import React, {Component, PropTypes} from 'react'
class DefaultPropsExample extends Component {
  static defaultProps = {
    ...Component.defaultProps,
    instructions: 'Usage instructions not provided.',
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete guide to React default props - LogRocket Blog
Cover three ways to implement default props in React and provide default values for props that are not required by the component.
Read more >
React: Everything about Default Props | by Chidume Nnamdi
In this article, we saw ways to set default values for the props argument in our React components be it ES6 class or...
Read more >
A complete guide to default props in React - SKPTRICKS
This tutorial explains how to use default props in react application. A React component is simply a JavaScript function that takes an object ......
Read more >
defaultProps vs ES6 default params when destructuring ...
Simply put, React offers a defaultProps attribute, to deal with default props values. And it is actually better to use it when dealing...
Read more >
ReactJS defaultProps - GeeksforGeeks
The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found