javascript – _react2.default.PropTypes.function未定义

前端之家收集整理的这篇文章主要介绍了javascript – _react2.default.PropTypes.function未定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个Button组件
  1. import React,{ Component } from 'react';
  2. import { View,Text,TouchableNativeFeedback } from 'react-native';
  3.  
  4. class Button extends Component {
  5. generateComponent() {
  6. const { buttonStyle,textStyle } = this.styles;
  7. const { text } = this.props;
  8.  
  9. switch (this.props.platform) {
  10. case 'android':
  11. return (
  12. <TouchableNativeFeedback onPress={this.props.onPress}>
  13. <View style={buttonStyle}>
  14. <Text style={textStyle}>{text}</Text>
  15. </View>
  16. </TouchableNativeFeedback>
  17. );
  18. case 'ios':
  19. return 0;
  20. }
  21. }
  22.  
  23. render() {
  24. return (
  25. <View>
  26. { this.generateComponent() }
  27. </View>
  28. );
  29. }
  30.  
  31. styles = {
  32. buttonStyle: {
  33. borderRadius: 100,justifyContent: 'center',height: parseInt(this.props.size,10),width: parseInt(this.props.size,backgroundColor: this.props.color,elevation: 3
  34. },textStyle: {
  35. fontWeight: 'bold',fontSize: parseInt(this.props.fontSize,lineHeight: parseInt(this.props.fontSize,10)
  36. + Math.floor(parseInt(this.props.fontSize,10) / 10) + 1,color: this.props.fontColor,textAlign: 'center'
  37. }
  38. }
  39. }
  40.  
  41. Button.propTypes = {
  42. text: React.PropTypes.string.isrequired,platform: React.PropTypes.string.isrequired,size: React.PropTypes.string.isrequired,color: React.PropTypes.string.isrequired,fontColor: React.PropTypes.string.isrequired,fontSize: React.PropTypes.string.isrequired,onPress: React.PropTypes.function.isrequired
  43. };
  44.  
  45. export default Button;

我在组件Home中调用此组件

  1. import React,{ Component } from 'react';
  2. import { View } from 'react-native';
  3. import Metas from '../components/Metas';
  4. import Button from '../components/Button';
  5.  
  6. export default class Home extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = { Metas: ['Meta 1','Meta 2','Meta 3'] };
  10. }
  11.  
  12. render() {
  13. return (
  14. <View>
  15. <Metas data={ this.state.Metas } />
  16. <Button
  17. text="+"
  18. platform={ this.props.platform }
  19. onPress={ this._handleButtonPress }
  20. size='50'
  21. fontSize='25'
  22. color='#FFD600'
  23. fontColor='white'
  24. />
  25. </View>
  26. );
  27. }
  28.  
  29. _handleButtonPress() {
  30. console.log("Hello!");
  31. }
  32. }
  33.  
  34. Home.propTypes = {
  35. platform: React.PropTypes.string.isrequired
  36. };

问题是,如果我包含PropTypes,我会收到一个错误,指出onPress道具未定义.

我不明白为什么我收到这个错误.我安慰记录了onPress的typeof并打印功能.

有什么建议??

解决方法

更新于2017年9月

从React 15.5开始,PropTypes已被移动到它自己的库中.像这样用它:

  1. import PropTypes from 'prop-types';
  2.  
  3. class Example extends React.Component {
  4. render() {
  5. return (
  6. <h1>{this.props.test}</h1>
  7. );
  8. }
  9. }
  10.  
  11. Example.propTypes = {
  12. test: PropTypes.string
  13. };

资料来源:https://facebook.github.io/react/docs/typechecking-with-proptypes.html

猜你在找的JavaScript相关文章