有人可以解释为什么我在导入 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;
定义 propTypes 时应使用驼峰命名法而不是 TitleCase。
这样做:
CustomBackground.propTypes = {...}
而不是
CustomBackground.PropTypes = {...}
这是完整代码
CustomBackground.propTypes={
children:PropTypes.element.isRequired,
}