首页 > 解决方案 > 错误:组件 [ComponentName] 声明了 `PropTypes` 而不是 `propTypes`。您是否拼错了属性分配?

问题描述

有人可以解释为什么我在导入 PropTypes 时收到此错误:

组件 CustomBackground 声明PropTypes而不是propTypes. 您是否拼错了属性分配?

自定义背景.js:

import PropTypes from 'prop-types';


const CustomBackground=({children})=>(
    <ImageBackground source={background} style={styles.imagebackground}>
        {children}
    </ImageBackground>
)

CustomBackground.PropTypes={
    children:PropTypes.element.isRequired,
}

export default CustomBackground;

标签: react-nativereact-propsreact-proptypesprop

解决方案


在定义 propTypes 时,您应该使用 camelCase 而不是 TitleCase。

做这个:

CustomBackground.propTypes = {...}

代替

CustomBackground.PropTypes = {...}

推荐阅读