getDefaultProps doesn't work with ES6 syntax; warning is not helpful
See original GitHub issueI’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:
- Created 8 years ago
- Comments:20 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For future visitors, ES6 syntax for latest version should be ( none of the above ):
Revised
defaultProps
just needs to be a static property on the component class. How you want to do this is up to you.Static property:
Static getter:
Assign a property: