React Native 0.57.8安卓项目运行报错:Can't find variable: ReactPropTypes
Why this happens
React moved PropTypes out of the core react package starting in v15.5, and by React 16.6.3 (which pairs with RN 0.57.8), React.PropTypes is completely removed. Many of the older third-party libraries in your package.json (like react-native-elements@0.19.1, react-native-material-dropdown@0.11.1, etc.) still reference React.PropTypes or ReactPropTypes directly, which causes the runtime error.
Solutions
1. Quick fix: Globally attach PropTypes to React
This is the fastest way to get your app running without modifying third-party code. Add these lines at the very top of your project's entry file (usually index.js or App.js):
import React from 'react'; import PropTypes from 'prop-types'; // Make React.PropTypes available for old libraries React.PropTypes = PropTypes;
This tricks older dependencies into finding the PropTypes they expect, and works well for maintaining legacy projects.
2. Install the standalone prop-types package
First, ensure the package is installed in your project (it might already be there, but double-check):
npm install prop-types --save # Or with yarn: yarn add prop-types
This is required for both the quick fix above and any future code changes where you need to use PropTypes.
3. Long-term fix: Update or patch problematic dependencies
If you want to eliminate the hack entirely, address the root cause in your third-party packages:
- Upgrade compatible library versions: Check the release notes for each outdated dependency to find versions that support RN 0.57.8 and use the standalone
prop-typespackage. For example,react-native-elementshas newer versions in the 0.19.x line or later that might fix this—just make sure they're compatible with RN 0.57.8. - Use
patch-packageto modify old libraries: For dependencies that haven't been updated, you can patch their code directly:- Install
patch-package:npm install patch-package --save-dev - Navigate to
node_modulesand find the library file that usesReact.PropTypesorReactPropTypes. Replace any references toReact.PropTypeswith imports fromprop-types:// Before import React from 'react'; const MyComponent = ({ prop }) => ...; MyComponent.propTypes = { prop: React.PropTypes.string }; // After import React from 'react'; import PropTypes from 'prop-types'; const MyComponent = ({ prop }) => ...; MyComponent.propTypes = { prop: PropTypes.string }; - Generate a patch file:
npx patch-package <name-of-the-library> - From now on, every time you run
npm installoryarn install, runnpx patch-packageto apply your fixes automatically.
- Install
4. Double-check your own code
Even though you said you've checked, do a quick search across your project for any instances of ReactPropTypes or React.PropTypes. Replace those with imports from prop-types and use PropTypes directly:
// Replace this import React from 'react'; MyComponent.propTypes = { name: React.PropTypes.string }; // With this import React from 'react'; import PropTypes from 'prop-types'; MyComponent.propTypes = { name: PropTypes.string };
内容的提问来源于stack exchange,提问作者Bram Smith

