Catalog
  1. 1. propsTypes的作用
  2. 2. 用法
  3. 3. 举个栗子
React中propsTypes类型检测的用法

propsTypes的作用

  • 在React中,propsType是用来对组件props做类型检查的

用法

  1. 引入校验模块 prop-types
  2. 定义Component,然后给Component上面挂propsType属性

举个栗子

1
2
3
4
5
6
7
8
9
10
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};
Author: Erealm
Link: http://erealmsoft.github.io/2019/09/27/react/react-props-type/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment