我想访问父组件中定义的子组件的 DOM 节点。在下面的示例中,我尝试了“try1”和“try2”两种方法,但都不起作用。如何获取“theDiv”参考的 DOM 节点?
<Form>
<Frame>
<div ref="thediv" />
</Frame>
</Form>
Form.render() {
return (
<Frame>
<div ref="theDiv" />
</Frame>
}
try1.Frame.componentDidMount() {
let theDiv = ReactDOM.findDOMNode(this.refs.theDiv);
}
try2.Frame.componentDidMount() {
React.Children.forEach(this.props.children, child => {
if (child.ref === "theDiv") {
let theDiv = ReactDOM.findDOMNode(child);
}
});
}