Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find variable: PropTypes

I am trying some react native code.

Added props for my component based on the instructions given in this blog.

End up in the error, Can't find variable: PropTypes Found the same question in github but it was closed without any answers

Couldn't get any clues.

like image 304
Sujananth Avatar asked Jan 01 '19 10:01

Sujananth


People also ask

Is PropTypes necessary in TypeScript?

Props are required in TypeScriptisRequired explicitly.

Is PropTypes recommended?

PropTypes are a good first line defense when it comes to debugging your apps. But before getting into detail about PropTypes, we have to understand the concept of props. Props are the read-only properties that are shared between components to give the unidirectional flow of React a dynamic touch.

What is PropTypes in TypeScript?

Typescript and PropTypes serve different purposes. Typescript validates types at compile time, whereas PropTypes are checked at runtime. Typescript is useful when you are writing code: it will warn you if you pass an argument of the wrong type to your React components, give you autocomplete for function calls, etc.

Can we use PropTypes in functional component?

In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.


1 Answers

You need to do install prop-types with npm install --save prop-types or yarn add prop-types and then add this to your code:

import PropTypes from 'prop-types';
like image 68
Canastro Avatar answered Sep 22 '22 15:09

Canastro