[英]How do I get the dom node of a child element in a component without adding logic to its parent/children?
In the following example WrapperComp
needs to get access to the dom node of the divs
in line 5 and line 8, without adding logic to PageComp
or ItemComp
. 在以下示例中,
WrapperComp
需要访问第5行和第8行中divs
的dom节点,而无需向PageComp
或ItemComp
添加逻辑。 The only things I could change in PageComp
are the div tags. 我在
PageComp
中唯一能改变的是div标签。 Eg I could add a ref
, prop
, data-attribute
, etc to them. 例如,我可以为它们添加
ref
, prop
, data-attribute
等。
The divs
don't have to be created inside PageComp
. 不必在
PageComp
内创建divs
。 WrapperComp
would be allowed to create them too, but they must wrap each of its children (In this case each ItemComp
). WrapperComp
也可以创建它们,但它们必须包装它的每个子ItemComp
(在本例中为每个ItemComp
)。
Example 例
class PageComp extends React.Component {
render() {
return (
<WrapperComp>
<div>
<ItemComp/>
</div>
<div>
<ItemComp/>
</div>
</WrapperComp>
);
}
}
class WrapperComp extends React.Component {
render() {
return (
<div>
<h1>A wrapper</h1>
{this.props.children}
</div>
);
}
}
class ItemComp extends React.Component {
render() {
return (
<div>
<h1>An item</h1>
</div>
);
}
}
ReactDOM.render(
<PageComp/>,
document.getElementById('app')
);
What I tried so far: 到目前为止我尝试了什么:
ref=
on the divs
, but that ref
would only be available in PageComp
not in WrapperComp
. divs
上放置一个ref=
,但是ref
只能在PageComp
而不能在WrapperComp
。 divs
inside WrapperComp
and put a ref=
on them from there, but that would result in a Refs Must Have Owner Warning WrapperComp
创建divs
在WrapperComp
放置一个ref=
但是这会导致Refs Must Have Owner警告 Now I wonder.. what would be an appropriate way in react to solve that problem? 现在我想知道..解决这个问题的反应是什么?
Till now the only solution that came to my mind was to put a data-attribute
on each div
and search the dom for them after componentDidMount
like that: document.querySelectorAll('[data-xxx]')
. 到目前为止,我想到的唯一解决方案是在每个
div
上放置一个data-attribute
,并在componentDidMount
之后搜索dom: document.querySelectorAll('[data-xxx]')
。 Perhaps I'm not sure if this is how you do it in react.. 也许我不确定你是否就这样做了反应..
Why do I want to get the node inside WrapperComp
? 为什么我想在
WrapperComp
获取节点?
I want to create a component that adjusts the dimensions of its children. 我想创建一个调整其子项维度的组件。 In the example that component would be
WrapperComp
. 在该示例中,组件将是
WrapperComp
。 The adjustments can only be done after the children rendered to the dom, eg to get clientHeight
. 只有在孩子们渲染到dom之后才能进行调整,例如获得
clientHeight
。
If you don't restrict that this needs to be solved by how one should get the DOM, pass them down, etc, I would get rid of the puzzle and approach it in a different direction. 如果你不限制这需要通过如何获得DOM,传递它们等来解决,我会摆脱困惑并以不同的方向接近它。
Since you are not given much control to <PageComp>
whereas <WrapperComp>
seems flexible, I would do the wrapping in the later by transforming the passed children to what you need them to be. 由于你没有对
<PageComp>
给予太多控制,而<WrapperComp>
似乎是灵活的,我会在后面通过将传递的子项转换为你需要它们来进行包装。
class PageComp extends React.Component {
render() {
return (
<WrapperComp>
<ItemComp/>
<ItemComp/>
</WrapperComp>
);
}
}
class WrapperComp extends React.Component {
render() {
const wrappedChldren = React.Children.map(this.props.children, function(child) {
return (
<div ref={function(div) {
this.setState{clientHeight: div.clientHeight}
}}>
<h1>A wrapper</h1>
{ child }
</div>
);
});
return <div>{ wrappedChildren }</div>;
}
}
With this concentrate can be put on the transformation in the <WrapperComp>
, which is pretty intuitive as its name suggests. 有了这个集中可以在
<WrapperComp>
,这正如其名称所暗示的那样非常直观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.