PropTypes React Native 不是对象

PropTypes React Native is not an object

我的代码在 React Native 中遇到 PropTypes 问题:

import React, { Component, PropTypes } from 'react';
import { Text } from 'react-native';

export default class Star extends Component {
    render() {
        return ( <Text> Hello </Text> );
    }
}

Star.propTypes = {
    fullStar: PropTypes.string.isRequired,
    halfStar: PropTypes.string.isRequired,
    emptyStar: PropTypes.string.isRequired,
    half: PropTypes.bool,
    count: PropTypes.number,
    size: PropTypes.number,
}

Star.defaultProps = {
    fullStar: "",
    halfStar: "",
    emtyStar: "",
    half: 'true',
    count: 5,
    size: 30,
}

我的错误是 undefined is not an object (evaluating '_react2.PropTypes.string')

感谢阅读 ;)

PropTypes 移动到单独的包中。使用 prop-types 包。

更多信息here

Note:

React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead. We provide a codemod script to automate the conversion.

是的,PropTypes 已从版本 15.x 中弃用 您需要安装软件包:

npm install prop-types

然后导入。

import PropTypes from 'prop-types';

有时候,如果您安装任何未使用最新的 React Native 更新的节点模块,那么您在导入 proptype 的每个 js 文件中都会发生变化

replace
import {PropTypes} from 'react';
with
import PropTypes from 'prop-types';

如果您遇到此类问题,这将会有所帮助。 在最新的 react native version(0.58.3) 中,Project included 'react-native-collapsible-bar' packages 有这个错误

Undefined is not an object(evaluating 'React.PropTypes.oneOf')

我发现这个模块也有以下依赖关系。

"prop-types": "^15.5.10",
"react-native-vector-icons": "^3.3.0"

首先,我在BarCollapsible.js

中评论了下面的代码
   static propTypes = {
         style: View.propTypes.style,
         titleStyle: Text.propTypes.style,
         tintColor: PropTypes.string,
   }

然后我卸载了 react-native-vector-icons 并用最新版本重新安装,终于摆脱了错误。

import PropTypes from 'prop-types';