Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropType not giving warning

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?????

like image 360
Dan Avatar asked Feb 11 '18 02:02

Dan


1 Answers

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.

like image 77
Striped Avatar answered Sep 28 '22 23:09

Striped