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.

getDefaultProps doesn't work with ES6 syntax; warning is not helpful

See original GitHub issue

I’m trying to build a component that defines a default value for a required prop, but it doesn’t seem to work.

class ReviewsList extends Component {
  propTypes: {
    reviews : React.PropTypes.array.isRequired,
  }
  getDefaultProps() {
    return {
      reviews: [],
    };
  }
  render() {
    var reviewItems = this.props.reviews.map(function (review) {  // ERROR
      return (
        <RecipeReviewItem key={review.objectId} reviewText={review.reviewText}></RecipeReviewItem>
      )
    });

    return (
      <View>{reviewItems}</View>
    );
  }
}

When I run this, I get:

ERROR: undefined is not an object (evaluating 'this.props.reviews.map')

Why?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

61reactions
seanfuturecommented, Apr 11, 2016

For future visitors, ES6 syntax for latest version should be ( none of the above ):

Revised

ReviewsList.propTypes = {
   reviews : React.PropTypes.array.isRequired,
}

ReviewsList.defaultProps = {
      reviews: [],
}
53reactions
idecommented, Aug 4, 2016

defaultProps just needs to be a static property on the component class. How you want to do this is up to you.

Static property:

class C1 extends React.Component {
  static defaultProps = {
    prop: 'hello',
  };
}

Static getter:

class C2 extends React.Component {
  static get defaultProps() {
    return {
      prop: 'hello',
    };
  }
}

Assign a property:

class C3 extends React.Component {
}
C3.defaultProps = {
  prop: 'hello',
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Without ES6
Autobinding. In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically ......
Read more >
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 >
defaultProps vs ES6 default params when destructuring ...
We don't have this issue with the address property since it's not ... It works exactly the same as "Stateless non-functional component".
Read more >
Reusable Components - react
As your app grows it's helpful to ensure that your components are used ... Don't `console.warn` or throw, as this // won't work...
Read more >
21 Essential React.js Interview Questions and Answers | Toptal®
Whether you're a candidate or interviewer, these interview questions will help prepare you for your next React.js interview ahead of time.
Read more >

github_iconTop Related Medium Post

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