简体   繁体   English

在反应中,'PropTypes.node`和`PropTypes.any`有什么区别?

[英]What is the difference between `PropTypes.node` and `PropTypes.any` in react?

// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
optionalNode: PropTypes.node,

// A value of any data type
requiredAny: PropTypes.any.isRequired,

Which types does PropTypes.any contain compared to PropTypes.node ? PropTypes.any相比, PropTypes.node包含哪些类型?

PropTypes are a way to validate the values that are passed in through our props. PropTypes是一种验证通过我们的道具传递的值的方法。

node We can pass anything that can be rendered, such as numbers, string, DOM elements, arrays, or fragments that contain them using the React.PropTypes.node. node我们可以使用React.PropTypes.node传递任何可以呈现的内容,例如数字,字符串,DOM元素,数组或包含它们的片段。

any type React allows us to specify that a prop must be present, regardless of it's type. 任何类型 React都允许我们指定必须存在prop,而不管它的类型。 We can do this by using the React.PropTypes.any validator. 我们可以使用React.PropTypes.any验证器来完成此操作。

PropTypes.node: any render-able value like numbers and string, that can actually be rendered on screen. PropTypes.node:任何可渲染的值,如数字和字符串,实际上可以在屏幕上呈现。

PropTypes.any: any type of value, even those are non-render-able like boolean. PropTypes.any:任何类型的值,即使是像布尔值那样不可渲染的值。

Incase of <div>{true}</div> JSX code, 包含<div>{true}</div> JSX代码,

booleanValue: PropTypes.node will give an error, while booleanValue: PropTypes.any will not give any such error. booleanValue: PropTypes.node将给出错误,而booleanValue: PropTypes.any将不会给出任何此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM