[英]How to avoid the error “Unknown prop <> on tag. Remove this prop from the element”?
Please, how can I rewrite this code another way to avoid the error below? 请,我该如何以另一种方式重写此代码,以避免出现以下错误?
render() {
const { children, ...props } = this.props;
return <div {...props} ref={this.setRef}>{children}</div>
}
I get this error: 我收到此错误:
Unknown prop onClickOutside
on tag. 标签上的未知道具
onClickOutside
。 Remove this prop from the element. 从元素中删除此道具。 For details ( https://facebook.github.io/react/docs/higher-order-components.html#static-methods-must-be-copied-over )
有关详细信息( https://facebook.github.io/react/docs/higher-order-components.html#static-methods-must-be-copied-over )
Native DOM elements only allowed to have Native DOM attributes. 仅允许本机DOM元素具有本机DOM属性。 You can't pass any attribute (prop) you want.
您不能传递所需的任何属性(属性)。
If you know what valid props you need to this element you can destruct them out of props and pass them explicit. 如果您知道此元素需要什么有效道具,则可以将它们从道具中破坏出来,并显式传递它们。
For example: 例如:
render() {
const { children, onClickOutside } = this.props;
return <div onClick={onClickOutside} ref={this.setRef}>{children}</div>
}
You can simply remove in from the ...props
you transfer: 您可以简单地从转移的
...props
删除:
render() {
const { children, onClickOutside, ...props } = this.props;
return <div {...props} ref={this.setRef}>{children}</div>
}
If you want onClickOutside
to map to the onClick
of the div
, then handle this explicitly: 如果您希望
onClickOutside
映射到div
的onClick
, onClickOutside
明确处理此问题:
render() {
const { children, onClickOutside, ...props } = this.props;
return <div {...props} onClick={onClickOutside} ref={this.setRef}>{children}</div>
}
Any version of React < 16.x, have a whitelist of available attributes. 任何版本的React <16.x,都有可用属性的白名单。 So, all attributes passed to elements must be in the whitelist.
因此,传递给元素的所有属性都必须在白名单中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.