Cannot read property 'string' of undefined | React.PropTypes | LayoutPropTypes.js
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
Answers
The issue you're encountering is related to the deprecation and removal of React.PropTypes
in React 16. Since React 16, PropTypes
are no longer part of the main react
package and have been moved to a separate prop-types
package.
In React Native 0.45.1, it seems the library is still referencing React.PropTypes
, which is causing the error because PropTypes
is not found in React 16.
Solution
You need to update your React Native version to a more recent one that supports React 16. Alternatively, if updating React Native is not an option, you can manually patch the code to use the prop-types
package.
Steps to Fix
-
Install
prop-types
Package: First, ensure you haveprop-types
installed in your project:npm install prop-types
-
Modify
LayoutPropTypes.js
: Opennode_modules/react-native/Libraries/StyleSheet/LayoutPropTypes.js
and update the import to use theprop-types
package.// Change this line // var ReactPropTypes = require('React').PropTypes; // To this line var PropTypes = require('prop-types');
-
Update References: Update any reference to
ReactPropTypes
in the file toPropTypes
.// Example: // height: ReactPropTypes.number, // Change to: height: PropTypes.number,
Example Patch
Here is a sample of how you might modify LayoutPropTypes.js
:
var PropTypes = require('prop-types');
var LayoutPropTypes = {
width: PropTypes.number,
height: PropTypes.number,
top: PropTypes.number,
left: PropTypes.number,
right: PropTypes.number,
bottom: PropTypes.number,
// Add other prop types as needed
};
module.exports = LayoutPropTypes;
Long-Term Solution
Updating your React Native version is the recommended long-term solution. To update React Native, you can follow the official React Native upgrade guide. Typically, you would update your package.json
dependencies and run npm install
or yarn install
.
{
"dependencies": {
"react": "^16.0.0",
"react-native": "^0.63.0"
}
}
Then, run:
npm install
# or
yarn install
After updating, make sure to test your application thoroughly, as major updates can introduce breaking changes.