为什么 React 不检查我的 propTypes?

Why is React not checking my propTypes?

我已阅读 the official documentation and the npm package description PropTypes。

然而,当我运行这段代码时,

const propTypes = {
    foo: PropTypes.string.isRequired,
    bar: PropTypes.number.isRequired,
    umu: PropTypes.string
};

const defaultProps = {
    umu: "default value"
};

class MyComp extends React.Component {
    constructor(props){
        super(props);
    }

    render() {
        return (
        <ul>
            <li>foo is {this.props.foo}</li>
            <li>bar is {this.props.bar}</li>
            <li>umu is {this.props.umu}</li>
        </ul>
        );
    }
}
MyComp.propTypes = propTypes;
MyComp.defaultProps = defaultProps;

class MyWrapper extends React.Component{
    render() {
        return (
            <div>
                <MyComp bar="string and not a number"/>
            </div>
        );
    }
}

ReactDOM.render(React.createElement(MyWrapper, null, null), document.getElementById("app"));

我的 propTypes 没有被选中,尽管所需的 属性 foo 没有设置并且 属性 bar 不是数字。谁能告诉我我做错了什么? 这里对应的是CodePen.

非常感谢。

即使验证失败,该组件也会呈现,这更像是对开发人员的警告,因此您会在控制台上看到错误。 CodePen 没有显示这些警告,但是,可能是因为您使用的是 React 的生产版本?无论哪种方式,这都是正确的行为。