摘要:类型检查是为了确保传入组件的参数正确性。通常在项目中可以使用或者来实现。示例以上内容在实现一个通用组件时非常有用。类型检查和参数默认值一起使用,保证组件的正常运行。
Typechecking With PropTypes
类型检查是为了确保传入组件的参数正确性。
通常在项目中可以使用Flow或者TypeScript来实现。
React提供了PropTypes来检查类型。
示例:
</>复制代码
import PropTypes from "prop-types";
class Greeting extends React.Component {
render() {
return (
Hello, {this.props.name}
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};
PropTypes
</>复制代码
import PropTypes from "prop-types";
MyComponent.propTypes = {
// You can declare that a prop is a specific JS type. By default, these
// are all optional.
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
optionalNode: PropTypes.node,
// A React element.
optionalElement: PropTypes.element,
// You can also declare that a prop is an instance of a class. This uses
// JS"s instanceof operator.
optionalMessage: PropTypes.instanceOf(Message),
// You can ensure that your prop is limited to specific values by treating
// it as an enum.
optionalEnum: PropTypes.oneOf(["News", "Photos"]),
// An object that could be one of many types
optionalUnion: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Message)
]),
// An array of a certain type
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
// An object with property values of a certain type
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
// An object taking on a particular shape
optionalObjectWithShape: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
}),
// You can chain any of the above with `isRequired` to make sure a warning
// is shown if the prop isn"t provided.
requiredFunc: PropTypes.func.isRequired,
// A value of any data type
requiredAny: PropTypes.any.isRequired,
// You can also specify a custom validator. It should return an Error
// object if the validation fails. Don"t `console.warn` or throw, as this
// won"t work inside `oneOfType`.
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
"Invalid prop `" + propName + "` supplied to" +
" `" + componentName + "`. Validation failed."
);
}
},
// You can also supply a custom validator to `arrayOf` and `objectOf`.
// It should return an Error object if the validation fails. The validator
// will be called for each key in the array or object. The first two
// arguments of the validator are the array or object itself, and the
// current item"s key.
customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
"Invalid prop `" + propFullName + "` supplied to" +
" `" + componentName + "`. Validation failed."
);
}
})
};
Requiring Single Child
</>复制代码
import PropTypes from "prop-types";
class MyComponent extends React.Component {
render() {
// This must be exactly one element or it will warn.
const children = this.props.children;
return (
{children}
);
}
}
MyComponent.propTypes = {
children: PropTypes.element.isRequired
};
Default Prop Values
</>复制代码
class Greeting extends React.Component {
render() {
return (
Hello, {this.props.name}
);
}
}
// Specifies the default values for props:
Greeting.defaultProps = {
name: "Stranger"
};
// Renders "Hello, Stranger":
ReactDOM.render(
,
document.getElementById("example")
);
以上内容在实现一个通用React组件时非常有用。类型检查和参数默认值一起使用,保证组件的正常运行。
推荐阅读《React 手稿》
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/108633.html
摘要:官方也陈述,接下来的的工作会投入到中。从目前官方的文档可以看出,从以下四个方面来提高的编码。生命周期自定义方法的主要用途是替代之前版本的组件。说明到目前为止,在已发布的版本中有该功能,想体验该功能,必须安装。 React Hooks React在16.7.0-alpha.0版本中提到了Hooks的概念,目前还是Proposal阶段。 官方也陈述,接下来的90%的工作会投入到React ...
摘要:属性是必须的。需要一个与的一致。必须在的返回原。调试插件日志安装组件。然后加入到中即可例如扩展程序需要在应用市场安装然后在中使用增强功能将加入即可在线实例推荐阅读手稿 React-Redux Redux Action function addTodo(text) { return { type: ADD_TODO, text } } type 属性是必须的。...
摘要:相当于一个放置在与中的垫片。之所以称之谓副作用呢,就是为了不让触发一个时,立即执行。也就是在与之间做一个事情,比如异步获取数据等。使用了中的功能,避免了像的回调地狱。把放入中最后再实现相就的即可在线示例推荐阅读手稿 Redux-Saga redux-saga 是一个用于管理应用程序副作用(例如异步获取数据,访问浏览器缓存等)的javascript库,它的目标是让副作用管理更容易,执行更...
摘要:示例使用场景代码复用类似版本之前的。多个组件同用一段代码,或者同样的方法时,可以使用。示例在线示例抽象和更改可以通过包裹的组件公共抽象出来。可以通过包裹的组件传递修改添加等的示例渲然劫持条件渲然。示例反向继承返回的高阶组件类继承了。 Higher-Order Components HOC 不是React的标准API。 HOC 是一个函数。 HOC 返回一个Component。 示例...
摘要:提供了除之外的传参数的方式。是全局跨组件传递数据的。在线示例推荐阅读手稿 Context Context提供了除props之外的传参数的方式。 Context是全局跨组件传递数据的。 API React.createContext const {Provider, Consumer} = React.createContext(defaultValue); Provider ...
阅读 1647·2021-11-22 09:34
阅读 1534·2021-09-22 14:57
阅读 3596·2021-09-10 10:50
阅读 1649·2019-08-30 15:54
阅读 3821·2019-08-29 17:02
阅读 3606·2019-08-29 12:54
阅读 2770·2019-08-27 10:57
阅读 3453·2019-08-26 12:24