PropType没有给出警告

Dan*_*Dan 5 javascript node.js reactjs create-react-app react-proptypes

我有一个节点项目,非常简单,我有一个页面,一个组件和一个index.js.我有两个道具:textnum.我正在尝试使用PropTypes来发出警告,如果它们不是正确的类型,或者当我这样做时它们不存在.isRequired.但是,他们没有抛出任何错误.这是我的PropTypes代码的问题吗?我使用的是React 16.2.0和prop-types 15.6.0.我曾经create-react-app创建我的应用程序.

这是代码.

App.js:

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

class App extends React.Component{
  render() {
    let text = this.props.text
    let num = this.props.num
    return <h1>{text}{num}</h1>
  }
}

App.propTypes = {
  text: PropTypes.string.isRequired,
  num: PropTypes.number
};

export default App
Run Code Online (Sandbox Code Playgroud)

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <App num="hey"/>,
  document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)

index.html的:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>React App</title>
</head>
<body>
  <div id="root"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,不仅num错误的类型(字符串而不是数字)而且text不存在,并且标记为.isRequired.

我的服务器继续运行并显示"嘿"并且不发出任何警告.我在这里失踪了什么?????

Str*_*ped 10

错误不会打印在终端中,而是打印在浏览器控制台中.并检查consol级别,您可能处于"警告"或其他状态.

  • 很好,但是我怎样才能在编译时得到警告。我正在使用纱线启动。 (2认同)
  • 这有点晚了,但有人设法找出如何在终端中打印错误吗? (2认同)