简体   繁体   English

流星中的React PropTypes对我不起作用

[英]React PropTypes in Meteor doesn't work for me

I tried different meteor/react tutorials and I always have the same problem. 我尝试了不同的流星/反应教程,但我始终遇到相同的问题。

I don't know why PropTypes don't display warnings in console if something is wrong. 我不知道如果出现问题,为什么PropTypes不在控制台中显示警告。 For example: 例如:

If I remove title <TitleBar title="Test title"/> The code still works normal without warnings even if it's required. 如果我删除标题<TitleBar title="Test title"/> ,即使需要该代码,也可以正常工作而不会发出警告。

If I change string in object, bool or array title: PropTypes.string.isRequired everything still works normal without warnings. 如果我更改对象,布尔值或数组title: PropTypes.string.isRequired字符串title: PropTypes.string.isRequired一切仍然正常,没有警告。

import React from 'react';
import PropTypes from 'prop-types';

export default class TitleBar extends React.Component {
  render(){
    return (
      <div>
        <h1>{this.props.title}</h1>
      </div>
    );
  }
}

TitleBar.propTypes = {
  title: PropTypes.string.isRequired
};

I also tried official Meteor/React tutorial and I got the same problem. 我也尝试了官方的“流星/反应”教程,但遇到了同样的问题。 https://www.meteor.com/tutorials/react/components https://www.meteor.com/tutorials/react/components

Is PropTypes just for testing purpose? PropTypes仅用于测试目的吗?

install the new package from npm 从npm安装新软件包

  npm install --save prop-types

and import this in your code 并将其导入您的代码中

  import React from 'react';
  import PropTypes from 'prop-types';
  export default class TitleBar extends React.Component {
  render() {
          return (
             <div>
             <h1>{this.props.title}</h1>

            </div>
                 );
      }
    }

    TitleBar.propTypes = {
        title: PropTypes.string.isRequired

      };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM