Cannot read property 'string' of undefined

2019-01-17 20:58发布

问题:

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

回答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.



回答2:

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.



回答3:

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
}


回答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.



回答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",