那些年,React 踩过的坑
发布时间
阅读量:
阅读量
1. propTypes 空数组报undefined Warning
举例来说:
import React from 'react';
import { Breadcrumb, Icon } from 'antd';
import PropTypes from 'prop-types'
const BreadcrumbBlock = ({ paths }) => {
const items = paths.map( (path, index) => (
<Breadcrumb.Item key={index+1}>
<span>{path}</span>
</Breadcrumb.Item>
));
return (
<Breadcrumb separator='>'>
<Breadcrumb.Item key="0">
<Icon type="home"/>
</Breadcrumb.Item>
{items}
</Breadcrumb>
);
}
Breadcrumb.propTypes = {
paths: PropTypes.array.isRequired
}
export default BreadcrumbBlock;
当处理 paths=[]的情况时,在控制台中会出现警告信息:指示 paths 的值为 undefined。因为如果不设置 isRequired 属性,在某些情况下其值也会被视为 undefined。即使不设置该属性,在某些情况下其值也会被视为 undefined。问题将迎刃而解
Breadcrumb.propTypes = {
paths: PropTypes.array
}
全部评论 (0)
还没有任何评论哟~
