ReactJs无法找到Ref

时间:2016-08-20 08:13:26

标签: javascript reactjs react-dom

我的目标:找到标签的参考并将其着色。使用的代码:

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

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

产生:Uncaught TypeError: Cannot read property 'color' of null

看起来它无法找到ref。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您需要在元素

中指定ref

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

但使用ref如下{/ 3}}

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

您可以将标签作为this.myLabel

进行访问