javascript react中`PropTypes.node`和`PropTypes.any`有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50206801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between `PropTypes.node` and `PropTypes.any` in react?
提问by yingting nie
// 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.anycontain compared to PropTypes.node?
PropTypes.any与 相比包含哪些类型PropTypes.node?
回答by Priya
PropTypesare a way to validate the values that are passed in through our props.
PropTypes是一种验证通过我们的 props 传入的值的方法。
nodeWe 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 typeReact allows us to specify that a prop must be present, regardless of it's type. We can do this by using the React.PropTypes.any validator.
任何类型的React 允许我们指定一个 prop 必须存在,而不管它的类型。我们可以通过使用 React.PropTypes.any 验证器来做到这一点。
回答by Nitin Jadhav
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.nodewill give an error, while
booleanValue: PropTypes.anywill not give any such error.
booleanValue: PropTypes.node将给出错误,而
booleanValue: PropTypes.any不会给出任何此类错误。

