React find DOM node by refs set to variable?

I dynamically create multiple text inputs (with dynamically generated refs) along the text I want to update with input.

I am trying to get the input value by setting ref to a variable and finding the DOM node with React.findDOMNode (this.refs.Variable) .value

I get a "Can not read property" message with zero error.

How can i achieve this?

This is what the function looks like:

resetUnit: function(e){ var refID = e.target.id; var ID = refID.split("-")[0]; var Value = React.findDOMNode(this.refs.refID).value; NodesCollection.update({_id: ID},{$set: { materialUnit : Value}}); this.setState({ edit: '' }); }, 
+5
source share
3 answers
 var Value = React.findDOMNode(this.refs.refID).value; 

finds the DOM node for a component with ref "refID" . If you want to find the DOM node for a component with ref refID (variable), you need

 var Value = ReactDOM.findDOMNode(this.refs[refID]).value; 
+9
source

I had to do

 import ReactDOM from 'react-dom'; ReactDOM.findDOMNode(this.refs.hi); 

in React 15.2.1 ( https://facebook.imtqy.com/react/docs/top-level-api.html#reactdom.finddomnode )

+2
source

I get a "Can not read property" message with zero error.

Two possible cases:

  • id is wrong. Check out the extra code or log to double check the id of what you think.
  • Called early: are you sure the componentDidMount call was called?
0
source

Source: https://habr.com/ru/post/1233668/


All Articles