美文网首页
props 的校验

props 的校验

作者: 未来在奋斗 | 来源:发表于2019-12-18 20:51 被阅读0次

props 的校验

自 React v15.5 起,React.PropTypes 已移入另一个包中。请使用 prop-types 库 代替。

使用

  1. 安装 prop-types
  2. 设置 组件的 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
  };
}

相关文章

网友评论

      本文标题:props 的校验

      本文链接:https://www.haomeiwen.com/subject/rzuvnctx.html