2016-08-20 120 views
0

我的目標:找到標籤的裁判和着色它。代碼中使用:ReactJs無法找到參考

colorizeLabel(){ 
    ReactDOM.findDOMNode(this.refs.amountLabel).color('#ffffff'); 
} 

<label itemRef="amountLabel">Choose Amount:</label> 

其產生:Uncaught TypeError: Cannot read property 'color' of null

它看起來是無法找到參考。我錯過了什麼嗎?

回答

1

您需要元素指定裁判

<label ref="amountLabel">Choose Amount:</label>

然而,它是advised使用ref如下

<label ref={(ref) => this.myLabel = ref} />

,您可以訪問標記爲this.myLabel

+0

此產品ReactDOM.findDOMNode(...)。color不是函數 – TeodorKolev

+0

您不能修改顏色一樣的..你需要使用道具/狀態來處理 – anoop

+0

檢查這個https://jsfiddle.net/3m8wLcgk/ – anoop