简体   繁体   English

如何在不向其父/子项添加逻辑的情况下获取组件中子元素的dom节点?

[英]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节点,而无需向PageCompItemComp添加逻辑。 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. 例如,我可以为它们添加refpropdata-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')
);

JSBIN JSBIN

What I tried so far: 到目前为止我尝试了什么:

  • I already tried to put a ref= on the divs , but that ref would only be available in PageComp not in WrapperComp . 我已经尝试在divs上放置一个ref= ,但是ref只能在PageComp而不能在WrapperComp
  • I also tried to create the divs inside WrapperComp and put a ref= on them from there, but that would result in a Refs Must Have Owner Warning 我也尝试在WrapperComp创建divsWrapperComp放置一个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.

相关问题 如何在 JavaScript 中将父级的值放入其子级函数中? - How do I get the value of the parent into its children function in JavaScript? 如何在 React 中将 dom 元素从子组件移动到父组件? - How to move a dom element from a child component to a parent component in React? 如何仅获取父元素的可见子元素? - How do I get only the visible children elements of a parent element? 如何获得子组件的节点/引用? - How do I get children component's node / ref? 在没有按钮 OnClick 事件的情况下,如何将存储在子组件中的 state 值传递给 React 中的父组件? - How do i get the state value which I have stored in the Child component to the Parent component in React without button OnClick events? 如何从子组件获取年龄数据到父组件 - How do I get the Age data from child to parent component 如何在事件侦听器中将父节点与其所有子节点相关联? jQuery / JavaScript - How do I associate a parent node with all of its children in an event listener? jQuery / JavaScript 如何在不清空子对象的情况下清空DOM对象? (dom-construct.empty(myDiv)在FF和Chrome中浅,在IE中深) - How do I empty a DOM object without emptying its children? (dom-construct.empty(myDiv) shallow in FF & Chrome, deep in IE) 如何将多个事件侦听器从React中的父组件附加到子组件的同一事件中? - How do I attach multiple event listeners to the same event of a child component from its parent component in React? React Native - 如何从父组件调用子组件的 function(不触发无限循环)? - React Native - how do you call the function of a child component from its parent (without triggering an infinite loop)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM