PropType 没有发出警告
PropType not giving warning
我有一个node项目,很简单,一个页面,一个组件,一个index.js
。我有两个道具:text
和 num
。如果它们不是正确的类型,或者当我这样做时它们不存在,我正在尝试使用 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
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App num="hey"/>,
document.getElementById('root')
);
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
如您所见,不仅 num
类型错误(字符串而不是数字),而且 text
不存在,并标记为 .isRequired
.
我的服务器继续 运行 并显示 "hey" 并且不发出任何警告。我在这里错过了什么?????
错误不会打印在您的终端中,而是打印在您的浏览器控制台中。并检查控制台级别,您可能处于 "warning" 或其他状态。
我有一个node项目,很简单,一个页面,一个组件,一个index.js
。我有两个道具:text
和 num
。如果它们不是正确的类型,或者当我这样做时它们不存在,我正在尝试使用 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
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App num="hey"/>,
document.getElementById('root')
);
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
如您所见,不仅 num
类型错误(字符串而不是数字),而且 text
不存在,并标记为 .isRequired
.
我的服务器继续 运行 并显示 "hey" 并且不发出任何警告。我在这里错过了什么?????
错误不会打印在您的终端中,而是打印在您的浏览器控制台中。并检查控制台级别,您可能处于 "warning" 或其他状态。