warning: React does not recognize the xxx prop on a DOM element

这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute。html

一般{...this.props}和cloneElement(element, this.props)这两种写法,会将父级别无用的attribute传递到子级的dom元素上。react

例如:dom

function MyDiv(props) { if (props.layout === 'horizontal') { // BAD! Because you know for sure "layout" is not a prop that <div> understands.
    return <div {...props} style={getHorizontalStyle()} />
  } else { // BAD! Because you know for sure "layout" is not a prop that <div> understands.
    return <div {...props} style={getVerticalStyle()} />
 } }

能够使用rest参数接收,删除等方法来解决:this

const { layout, ...rest } = props //或者
const divProps = Object.assign({}, props); delete divProps.layout;

具体可参考:React官方文档 Unknown Prop Warningurl

相关文章
相关标签/搜索