props 的校验
自 React v15.5 起,React.PropTypes 已移入另一个包中。请使用 prop-types 库 代替。
使用
- 安装 prop-types
- 设置 组件的 propTypes 的静态属性
import a from "prop-types";
class Hello extends React.Component {}
Hello.propTypes = {
prop1: a.string
};
// ====
const Hello = () => {};
Hello.propTypes = {
prop1: a.string
};
// ==== 如果开启了 public class field 语法,那么类组件可以
class Hello extends React.Component {
static propTypes = {
prop1: a.string
};
}
网友评论