Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  70] [ 4]  / answers: 1 / hits: 17818  / 9 Years ago, tue, june 9, 2015, 12:00:00

I am trying to access all ref values in my component and do something with them (for example, create payload to send to a server)



I was trying to do a simple for..in loop and than use getDOMNode().value on every ref but it doesn`t work.



var Hello = React.createClass({

getAllRefsValues: function() {
for(ref in this.refs) {
console.log(ref);
// ref.getDOMNode().value doesnt work here
}
},
render: function() {
return (
<div>
<button onClick={this.getAllRefsValues}>Get all props values</button>
<input type=text ref=test1/>
<input type=text ref=test2/>
<input type=text ref=test3/>
</div>
)
}
});


Here is jsfiddle I am working with.



I have a feeling that, this might not be a good idea to do, but I have no idea how to approach this atm.



Anyone help ?


More From » reactjs

 Answers
20

This is because this.refs is an object, you need to get the values, not the keys:



getAllRefsValues: function() {
for (var ref in this.refs) {
console.log(this.refs[ref]);
console.log(this.refs[ref].getDOMNode());
}
}


In my experience anyway, it is better to use Controlled Components over refs.


[#66270] Sunday, June 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;