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

相關文章
相關標籤/搜索