Reactjs: pass in variable to findDOMNode

By : akbarry

Running ES6 on top of Reactjs. Suppose I want to reference a node with firstName like this:

var el = React.findDOMNode(this.refs.firstName);

but type = 'firstName'. So I need to do something like this:

var el = React.findDOMNode(this.refs.{type});

And I've tried a handful of different variations and nothing works. I know the answer is very simple but I can't find it as I'm very much a newbie! I think this relates to string literals? Thanks in advance for your help!

By : akbarry


In this case, you want to make use of javascripts bracket notation for accessing object properties

this.refs[type] instead of this.refs.type

By : PhilVarg

This video can help you solving your question :)
By: admin