React 是否警告组件不期望的额外道具?

Does React warn of additional props that a component wasn't expecting?

如果我将 prop bar 传递给下面的组件,React 会抛出一个 warning/error 吗?

class MyComponent extends Component {
  ...
}

MyComponent.PropTypes = {
  foo: PropType.string,
};

不,它只会在您传递 propTypes 中定义的不具有预期类型的​​道具时发出警告。

如果你想收到关于此的警告,你可以使用 prop-types 包的 custom Airbnb implementation,其中包含一个验证器 forbidExtraProps

您的示例代码中也有错字。组件上的 proptypes 对象需要以小写 p 开头才能使其工作:

MyComponent.propTypes = {
  foo: PropTypes.string,
};