Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Proptypes' is not defined

I'm setting up a new React with the help of: Create React App

However, I'm running into a linting issue. I'm receiving the following linting error 'PropTypes' is not defined. (no-undef).

Here is the code that is causing the issue:

import React, { Component } from 'react'; import PropTypes from 'prop-types';  class Routers extends Component {   static propTypes = {     history: PropTypes.object.isRequired   };  ... 

I tried playing around with the react/prop-types rule, but to no avail.

like image 374
Lars Avatar asked Aug 15 '17 12:08

Lars


People also ask

How do you define an object in PropTypes?

propTypes = { //// key is the name of the prop and // value is the PropType } export default Count; PropTypes are also objects with a key and a value pair where the 'key' is the name of the prop while the value represents the type or class by which they are defined.

Is PropTypes necessary in TypeScript?

Building a component library This means users of your library cannot depend on your TypeScript type definitions for props, reiterating the relevance of PropTypes. PropTypes is normal JavaScript code, which means validations will also be possible when your library is being used.

Is PropTypes necessary in React?

One of the most important things when building React application is to make sure that the components receive correct props. Passing wrong props leads to bugs and unexpected behavior, so it's a good idea to warn developers about this as early as possible.

Is PropTypes deprecated?

PropTypes is deprecated since React 15.5.


2 Answers

Since react 15.5, PropTypes is included in a separate package, 'prop-types'. So this line will help

import PropTypes from 'prop-types' 

You can read more here

like image 62
Ratul Bhattacharjee Avatar answered Sep 22 '22 08:09

Ratul Bhattacharjee


According to this issue comment.

It appears to be because you have installed eslint 4.x when you should just use the eslint version that is shipped with create-react-app. You should remove any eslint you have manually installed and use the one that comes with the repo.

like image 32
Purgatory Avatar answered Sep 18 '22 08:09

Purgatory