首页 > 解决方案 > PropTypes 没有显示任何无效类型的警告 - React

问题描述

我是第一次实现 PropTypes,但它没有按预期验证道具。当存在不匹配的数据类型或没有传递数据时没有警告,并且只显示文本。以下是组件的代码:

组件:

class Counter extends React.Component {

  constructor() {
    super();
    this.state = {
      count: 0
    };
  }

  /** some other code **/

  render() {
    return (
      <View style={styles.appContainer}>
        <Text style={styles.count}>{this.state.count}</ Text>
        <Text>{this.props.num}</Text>
      </View>
    );
  }
}

Counter.propTypes = {
  num: PropTypes.number.isRequired
}

这是使用该组件的地方:

<View style={styles.appContainer}>
  <Button title="toggle" onPress={this.toggleCounter}/>
  <Counter num={"hi"}/>
</View>

标签: reactjsreact-native

解决方案


推荐阅读