我正在使用带有PropTypes和钩子的React
“ prop-types:” ^ 15.7.2“,
“反应”:“ ^ 16.11.0”,
而且我有一个从数据库接收带有一些数据的对象的组件。然后,测试数据:
{ console.log('Carousel reciving: ', typeof informations, informations) }
提供以下控制台日志:
Carousel reciving: object (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
数据通过调用发送到组件:
<Carousel informations={informations} isLoading={isLoading} />
和组件:
const Carousel = ({ informations, isLoading }) => (
<ReactPlaceholder ...some props />...some content</ReactPlaceholder>
) // Carousel
但是无论我是否将PropType更改为数组或对象,它仍然会出现以下错误:
警告:道具类型失败:提供给
informations
的类型为array
的道具Carousel
无效,预期为object
。
如果更改为PropType.object.isRequired,则错误表明所提供的值是数组类型。而且,如果我更改为PropType.array.isRequired,则错误提示所提供的值是对象类型:
Carousel.propTypes = {
informations: PropTypes.object.isRequired,
isLoading: PropTypes.bool.isRequired,
} // propTypes
我知道我可以在组件内部使用fetch函数,但是此数据与其他组件共享。
从您的console.log
开始,informations
实际上是一个对象数组;它记录为[{…}, {…},
,请注意开头[
。
所以您想将propTypes
更改为
Carousel.propTypes = {
informations: PropTypes.arrayOf(PropTypes.object).isRequired,
isLoading: PropTypes.bool.isRequired,
}