【发布时间】:2019-11-22 08:20:03
【问题描述】:
【问题讨论】:
【问题讨论】:
react/prop-types 仅检查是否设置了 prop-types。
react/require-default-props 强制开发人员为每个 prop 设置默认值。
const HelloWorld = ({ name }) => (
<h1>Hello, {name}!</h1>
);
// eslint react/prop-types will complain if you leave out this block
HelloWorld.propTypes = {
name: PropTypes.string
};
// eslint react/require-default-props checks for the following block
HelloWorld.defaultProps = {
name: 'john'
};
ReactDOM.render(<HelloWorld />, document.getElementById('app'));
【讨论】:
他们不做同样的事情。
第一个是 eslint-rule 检查使用的 props 是否也在 prop-types 中定义。第二个是在defaultProps
【讨论】: