Cannot read property 'string' of undefined | React.PropTypes | LayoutPropTypes.js

41,142

Solution 1

React.PropTypes is now deprecated:

Note: React.PropTypes is deprecated as of React v15.5. Please use the prop-types library instead.

You need to add the prop-types package separately now. The error most likely just started to show up because you deleted your node_modules folder and then reinstalled everything which upgraded your react version.

Solution 2

React is no more shipped with PropTypes. You will need to install it.

First install the prop-types package by running npm i prop-types --save.

Next use the prop-types package into your component like this:

import React from 'react'
import PropTypes from 'prop-types'

export const AwesomeComponent = props => {
    return(
        <h1>Hello {props.name}</h1>
    )
}

AwesomeComponent.propTypes = {
    name: PropTypes.string.isRequired
}

Or simply use an interface if you are using Typescript like this:

import * as React from 'react'

interface IAwesomeComponentProps {
    name: string
} 

export const AwesomeComponent: React.FC<IAwesomeComponentProps> = props => {
    return(
        <h1>Hello {props.name}</h1>
    )
}

Solution 3

Are you absolutely sure you are using react 16.0.0-alpha.12?

Check your package.json if you have a ^ before the react version, if you have, it probably have installed the latest react version, which currently is 16.0.0-alpha.13, in which it breaks as you say (just had the problem myself). Having the ^ before the version, allows it to install newer minor and patch versions. You can read more about it here.

To keep it at the exact version you specify, simply remove the ^ before the version, so that your package.json looks like this:

  "dependencies": {
    "react": "16.0.0-alpha.12",
    "react-native": "0.45.1",
  }

Remember to re-install your node_modules after your changes.

Solution 4

I have the similar problem, no solution. Answers talking about 'prop-types' package are meaningless, the problem is come from react-native source code. That is not an option to manually fix react native source in node_modules folder.

Solution 5

Now you can update to react-native 0.46.4, just follow along this guide: https://facebook.github.io/react-native/docs/upgrading.html

my package.json looks like this:

"react": "16.0.0-alpha.12",
"react-native": "0.46.4",
Share:
41,142

Related videos on Youtube

Nicolas Meienberger
Author by

Nicolas Meienberger

Updated on October 14, 2020

Comments

  • Nicolas Meienberger
    Nicolas Meienberger over 3 years

    After deleting and reinstalling my node_modules folder, I'm facing an issue that I don't understand in LayoutPropTypes.js file.

    In node_modules/react-native/Libraries/StyleSheet/LayoutPropTypes.js The following variable is undefined : var ReactPropTypes = require('React').PropTypes;

    react-native: 0.45.1 react: 16.0.0-alpha.12

  • Wendy Chen
    Wendy Chen almost 7 years
    I have added prop-types into my project, but I still have this problem. I tried to reinstall node, it was no use. And now, I even couldn't run the demo of react native. I want to know how can I degrade the version of react?
  • Wendy Chen
    Wendy Chen almost 7 years
    Fixed by restarting computer :(
  • Saltymule
    Saltymule almost 7 years
    @NicolasMeienberger yarn add [email protected]
  • skd
    skd almost 7 years
    What I do not fully understand, if I create a fresh app with react-native init then the app runs well.
  • 羅旭辰
    羅旭辰 almost 7 years
    I have the same issue in alpha.13, when back to alpha.12 is OK!