ReactJs 无法找到 Ref
ReactJs unable to find Ref
我的目标:找到标签的引用并给它上色。使用的代码:
colorizeLabel(){
ReactDOM.findDOMNode(this.refs.amountLabel).color('#ffffff');
}
<label itemRef="amountLabel">Choose Amount:</label>
产生:Uncaught TypeError: Cannot read property 'color' of null
好像找不到ref。我错过了什么吗?
需要在元素中指定ref
<label ref="amountLabel">Choose Amount:</label>
然而advised使用ref
如下
<label ref={(ref) => this.myLabel = ref} />
并且您可以访问标签 this.myLabel
我的目标:找到标签的引用并给它上色。使用的代码:
colorizeLabel(){
ReactDOM.findDOMNode(this.refs.amountLabel).color('#ffffff');
}
<label itemRef="amountLabel">Choose Amount:</label>
产生:Uncaught TypeError: Cannot read property 'color' of null
好像找不到ref。我错过了什么吗?
需要在元素中指定ref
<label ref="amountLabel">Choose Amount:</label>
然而advised使用ref
如下
<label ref={(ref) => this.myLabel = ref} />
并且您可以访问标签 this.myLabel