getDefaultProps doesn't work with ES6 syntax; warning is not helpful
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?
Upon inspecting the console log I got this message:
"Warning: getDefaultProps was defined on Blah, a plain JavaScript class. This is only supported for classes created using React.createClass. Use a static property to define defaultProps instead."
What does that even mean? What's a static property? How do you 'define defaultProps'? An example is worth its weight in gold here.
I've just learned that ES6 has the static keyword, but seriously, what's a static property?
I've tried
static defaultProps: {
reviews: []
}
and
static defaultProps() {
return {
reviews: []
};
}
But neither of these have worked.
static defaultProps = {
reviews: []
}
should work
try this
found solution from http://www.newmediacampaigns.com/blog/refactoring-react-components-to-es6-classes
Original
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>
);
}
}
New
class ReviewsList extends Component {
render() {
var reviewItems = this.props.reviews.map(function (review) { // ERROR
return (
<RecipeReviewItem key={review.objectId} reviewText={review.reviewText}></RecipeReviewItem>
)
});
return (
<View>{reviewItems}</View>
);
}
}
ReviewsList.propTypes: {
reviews : React.PropTypes.array.isRequired,
}
ReviewsList.getDefaultProps() {
return {
reviews: [],
};
}
Thanks @cyprusglobe. Fantastic answer, and a really informative link. It's working very well now, thanks. When RN moves towards ES6 in earnest the documentation examples must be updated to show this.
+1 @cyprusglobe
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: [],
}
isRequired doesn't throw an error when the prop isn't provided... I've had this issue with RN ES6 for a long time. Am I missing something?
for future users here is es2015 syntax for defaultprops...
static get defaultProps() {
return {
yourkey: yourvalue
}
}
@AlexChaseJones your suggestion works like a charm.
People on facebook could update their official docs at http://facebook.github.io/react-native/docs/using-navigators.html
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',
};
Last suggestion is the good one. Please update documentation as said by @liperuf
It has been updated on github as of: https://github.com/facebook/react-native/commit/9864c3a0dfe0aeb0cd9633e9adb381a3388de9bf#diff-a827bf1fa682284e142f4dd5bdb741d2
@ide very good! nice.
At what proposal stage is the static getter syntax? Is that stable enough to confidently implement?
It's at Stage 2 and the champion is currently at Facebook. I think the syntax might change (or might not) but you could write a codemod or similar source transformer to transpile from old syntax to new if that happened.
@ide you saved the day
static get defaultProps() { return { yourkey: yourvalue } }
I tried to find some more info about contextTypes, propTypes and defaultProps at React Native doc but I failed, could you tell me where we can find a doc for them?
works like charm with es6
class ReviewsList extends Component {
static propTypes = {
reviews : PropTypes.array.isRequired,
};
static defaultProps = {
reviews: [],
};
render() {
var reviewItems = this.props.reviews.map(function (review) { // ERROR
return (
<RecipeReviewItem key={review.objectId} reviewText={review.reviewText}></RecipeReviewItem>
)
});
return (
<View>{reviewItems}</View>
);
}
}