问题
Suppose I've got comments
list component with comments
components. I wanna implement method that will return all comments
components. I assigned to each comment
component same ref:
<comments>
<comment ref="myComments" text="abc" />
<comment ref="myComments" text="efg" />
</comments>
I thought I can access all my components by this.refs.myComments
but it doesn't work - it returns only last comment component.
What's the correct way to access all comment
components?
回答1:
There is no correct way to do that.
Your view is a representation of your data, so if you want the text for all comments, look at the data.
If you want to update the comments, update the data.
Pulling data out of the view, or manually manipulating the view defeats the purpose of react.
来源:https://stackoverflow.com/questions/26610392/reactjs-how-to-access-all-child-components-in-parent-component