Search code examples
javascriptreactjsreact-dom

ReactJs unable to find Ref


My target: to find ref of label and colorize it. Code used:

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

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

which produce: Uncaught TypeError: Cannot read property 'color' of null

It looks like it is unable to find ref. Do I miss anything?


Solution

  • You need to specify ref in element

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

    However it is advised to use ref as below

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

    and you can access label as this.myLabel