react-native icon indicating copy to clipboard operation
react-native copied to clipboard

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

Open hokster7 opened this issue 11 years ago • 20 comments

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?

hokster7 avatar Jun 28 '15 17:06 hokster7

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?

hokster7 avatar Jun 28 '15 17:06 hokster7

I've tried

  static defaultProps: {
      reviews: []
  }

and

  static defaultProps() {
    return {
      reviews: []
    };
  }

But neither of these have worked.

hokster7 avatar Jun 28 '15 17:06 hokster7

  static defaultProps = {
      reviews: []
  }

should work

andreypopp avatar Jun 28 '15 17:06 andreypopp

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: [],
    };
}

cyprusglobe avatar Jun 28 '15 17:06 cyprusglobe

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.

hokster7 avatar Jun 28 '15 19:06 hokster7

+1 @cyprusglobe

vhmth avatar Jan 29 '16 22:01 vhmth

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: [],
}

seanfuture avatar Apr 11 '16 14:04 seanfuture

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?

LightningK0ala avatar May 11 '16 11:05 LightningK0ala

for future users here is es2015 syntax for defaultprops...

static get defaultProps() {
  return {
       yourkey: yourvalue
  }
}

AlexChaseJones avatar Jun 15 '16 19:06 AlexChaseJones

@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

liperuf avatar Aug 04 '16 14:08 liperuf

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',
};

ide avatar Aug 04 '16 18:08 ide

Last suggestion is the good one. Please update documentation as said by @liperuf

angelo24983 avatar Aug 09 '16 12:08 angelo24983

It has been updated on github as of: https://github.com/facebook/react-native/commit/9864c3a0dfe0aeb0cd9633e9adb381a3388de9bf#diff-a827bf1fa682284e142f4dd5bdb741d2

danoz73 avatar Aug 17 '16 00:08 danoz73

@ide very good! nice.

yanbober avatar Sep 17 '16 06:09 yanbober

At what proposal stage is the static getter syntax? Is that stable enough to confidently implement?

ConAntonakos avatar Sep 28 '16 16:09 ConAntonakos

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 avatar Sep 28 '16 18:09 ide

@ide you saved the day

noahehall avatar Dec 19 '16 20:12 noahehall

static get defaultProps() { return { yourkey: yourvalue } }

libin1991 avatar Mar 26 '17 09:03 libin1991

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?

golestanirad avatar Oct 15 '17 18:10 golestanirad

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>
    );
  }
}

naiduprakash avatar Jan 13 '18 08:01 naiduprakash