以此代码 stub 为例:
class Wizard extends Component {
constructor(props) {
super(props);
this.state = {
steps: this.props.children ? this.props.children.length : null
}
}
componentWillMount() {
if (this.state.steps === null) {
throw new Error(
"The <Wizard /> component requires <Step /> components as children"
);
}
}
}
这应该是什么类型的错误?我觉得应该有一个 RequirementError,但我从来没有听说过。
最佳答案
所以我相信这是一个 PropType 错误,并由 React 的 PropTypes 模块处理。这是文档页面的链接:
PropTypes 将对组件的 props(适当命名)运行类型检查,并根据它们是否缺少必需的键抛出信息/错误消息。
代码示例:
import PropTypes from 'prop-types';
class MyComponent extends React.Component {
render() {
// This must be an array of children or it will warn.
const children = this.props.children;
return (
<div>
{children}
</div>
);
}
}
MyComponent.propTypes = {
children: PropTypes.arrayOf(PropTypes.element).isRequired
};
编辑:脱离了 React 的上下文,我认为错误类型将是 ChildType 错误
关于javascript - 这应该是什么类型的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599997/