2017-02-21 59 views
1

我想創建一個:indeterminate我的收音機和複選框的輸入元素屬性。未知支柱與反應,如何創建新的html屬性

我已經拋出了以下錯誤反應:

Unknown prop `indeterminate` on <input> tag. Remove this prop from the element 

是否有可能創造新的道具或已完全鎖定?

回答

1

簡單的包裹輸入到另一個組件,可以接受indeterminate道具:

const IndeterminateInput = React.createClass({ 
    render() { 
    // Create props clone 
    const cleanProps = Object.assign({}, this.props); 
    delete cleanProps['indeterminate']; 
    return <input ref="input" {...cleanProps}/> 
    }, 
    updateInput: function() { 
    this.refs.input.indeterminate = Boolean(this.props.indeterminate); 
    }, 
    componentDidMount() { 
    // Initial render 
    this.updateInput(); 
    }, 
    componentDidUpdate() { 
    // Props change 
    this.updateInput();  
    } 
}); 

然後你可以使用這個組件作爲一個包裝:

<IndeterminateInput type="checkbox" indeterminate /> 

注意到,即使在HTML indeterminate不一個有效的屬性。您必須手動更新DOM:

<!-- This does not work! --> 
<input type="checkbox" indeterminate>