最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PropType not giving warning - Stack Overflow

programmeradmin0浏览0评论

I have a node project, very simple, in which I have one page, one component, and one index.js. I have two props: text, and num. I am trying to use PropTypes to give a warning if they are not the correct type, or if they do not exist when I do .isRequired. However, they are not throwing any errors. Is this an issue with my code of PropTypes? I am using React 16.2.0, and prop-types 15.6.0. I used create-react-app to create my app.

Here is the code.

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>

As you can see, not only is num the wrong type, (string instead of number) but also, text does not exist, and it is marked as .isRequired.

My server continues to run and display "hey" and throw no warnings. What am I missing here?????

I have a node project, very simple, in which I have one page, one component, and one index.js. I have two props: text, and num. I am trying to use PropTypes to give a warning if they are not the correct type, or if they do not exist when I do .isRequired. However, they are not throwing any errors. Is this an issue with my code of PropTypes? I am using React 16.2.0, and prop-types 15.6.0. I used create-react-app to create my app.

Here is the code.

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>

As you can see, not only is num the wrong type, (string instead of number) but also, text does not exist, and it is marked as .isRequired.

My server continues to run and display "hey" and throw no warnings. What am I missing here?????

Share Improve this question asked Feb 11, 2018 at 2:52 DanDan 4373 silver badges13 bronze badges 9
  • I just setup this exact scenario and I get the warning in the console – Anthony Commented Feb 11, 2018 at 2:58
  • It will still render fine; you will just see red text in the developer console – Anthony Commented Feb 11, 2018 at 3:00
  • Well...did you create the app with create-react-app? @Tony – Dan Commented Feb 11, 2018 at 3:00
  • yeah, create-react-app @Dan – Anthony Commented Feb 11, 2018 at 3:01
  • I run npm start from my-app directory. I don't see an error at all. – Dan Commented Feb 11, 2018 at 3:01
 |  Show 4 more comments

1 Answer 1

Reset to default 20

The errors will not be printed in your terminal but in your browser console. And check the consol level, you might be in "warning" or something else.

发布评论

评论列表(0)

  1. 暂无评论