在React Js中,DOM组件是指在虚拟DOM中表示实际HTML元素的部分。

huangapple go评论69阅读模式
英文:

What are DOM components in the context of refs in react Js?

问题

在传统的React文档中关于ref forwarding有一个注释,其中提到:

Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.

在另一个Stack Overflow帖子中建议,DOM components 是那些返回单一的(非嵌套的)DOM元素的组件。例如:

function FancyButton(props) {
  return (
    <button className="FancyButton">
      //没有进一步的子元素
      //这意味着甚至没有 {props.children}
    </button>
  );
}

我的疑问是,这些 plain-single-non-nested(leaf element?) 元素是否也可以表示为类组件,所以如果React文档指的是DOM组件,那么为什么会说 You can forward refs to class component instances, too

对我来说,React文档似乎是想说:

ref forwarding is not limited to function components. You can forward refs to class component instances, too.

英文:

In the legacy react documentation for ref forwarding, there's a note which says:

> Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.

In another SO post it is suggested that DOM components are those which return a single(non-nested) dom element. E.g.

function FancyButton(props) {
  return (
    &lt;button className=&quot;FancyButton&quot;&gt;
      //no further child elements
      //which means not even {props.children}
    &lt;/button&gt;
  );
}

My doubt is that these plain-single-non-nested(leaf element?) elements can be represented as class components as well, so if react docs mean this as the DOM component, then why would it say You can forward refs to class component instances, too?

To me it feels like react docs meant to say

> ref forwarding is not limited to function components. You can forward refs to class component instances, too.

答案1

得分: 3

你说得对,术语“DOM组件”的含义可能不够明确,而且“DOM组件”和React树叶节点的概念之间存在一些重叠。

一般来说,DOM组件是指在页面上呈现实际HTML元素的任何组件。包括返回单个非嵌套元素的函数组件以及呈现一个或多个元素的类组件。

在“ref forwarding”的上下文中,React文档特别指的是呈现实际HTML元素的组件,无论它们是实现为函数组件还是类组件。Ref forwarding允许你将ref从父组件传递到底层子组件,以便直接与其底层DOM节点进行交互。

因此,当文档说“Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too”时,它意味着你可以将ref从父组件传递到呈现实际HTML元素的子类组件

总之,在术语“DOM组件”方面可能存在一些模糊之处,但在这个上下文中,它特指呈现实际HTML元素的组件,可以包括函数和类组件。

英文:

You are correct that the term "DOM component" can be ambiguous, and there is some overlap between the concept of a DOM component and a React tree leaf node.

A DOM component, in general, is any component that renders an actual HTML element on the page. Both function components that return a single non-nested element and class components that render one or more elements are included.

In the context of ref forwarding, the React documentation is specifically referring to components that render an actual HTML element, whether they are implemented as function components or class components. Ref forwarding allows you to pass a ref from a parent component to an underlying child component so that you can interact with its underlying DOM node directly.

So when the documentation says "Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too", it means that you can forward refs from parent components to child class components that render actual HTML elements.

In summary, while there may be some ambiguity in the term "DOM component", in this context it refers specifically to components that render actual HTML elements and can include both function and class components.

答案2

得分: 1

Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.

因此,基本上这里的重点不应该放在 class component 上,而应该放在 class component instances 上。

基本上,正如 @Shivanshu 在他的答案中所述,我们可以在直接称为 DOM 组件的 HTML 元素中使用 refs,或者我们可以进一步将其转发给子组件,无论是函数组件还是类组件。

根据我的理解,这里提到 class component instances 是因为如果我们将 refs 转发给类组件,它们需要使用 this 关键字访问该类的实例。

class ClassComponent extends React.Component {
  render() {
    return (
      <input
        value={this.props.value}
        onChange={this.props.onChange}
        ref={this.props.innerRef}
      />
    );
  }
}

const Input = React.forwardRef(function (props, ref) {
  return <ClassComponent {...props} innerRef={ref} />;
});
英文:

> Ref forwarding is not limited to DOM components. You can forward refs
> to class component instances, too.

So , basically here the focus should not be on the class component , it should be on the class component instances.

Basically, as stated by @Shivanshu in his answer , we can use refs in the HTML elements referred to as DOM Components directly or we can further forward it to the child components either it is a functional component or class component.

As per my understanding ,here the mention of class component instances is there because if we forward refs to class components , they are to be accessed using the instance of the class with this keyword.

class ClassComponent extends React.Component {
      render() {
        return (
          &lt;input
            value={this.props.value}
            onChange={this.props.onChange}
            ref={this.props.innerRef}
          /&gt;
        );
      }
    }

    

   const Input = React.forwardRef(function (props, ref) {
           return &lt;ClassComponent {...props} innerRef={ref} /&gt;;
   });

答案3

得分: 0

我开始认为,从 将引用转发给DOM组件 文章的上下文来看,他们将函数组件与DOM组件视为同义词

甚至更进一步,听起来他们认为叶子组件是函数组件的子集,尽管这是一个模糊的说法,因为他们从未定义叶子组件,你也可以基于类组件创建叶子组件。在引用转发的文章中,他们提到:

尽管对于应用程序级组件(如FeedStory或Comment)希望进行封装,但对于高度可重用的“叶子”组件(如FancyButton[明显是函数组件]或MyTextInput)可能会不方便。

另外,叶子组件可以具有纯DOM元素的嵌套结构,只是它们不能有进一步的React组件作为子组件。

现在文章的第一行是:

考虑一个渲染本机按钮DOM元素的FancyButton组件

在这里,FancyButton 是一个无状态<sup>1</sup>函数组件。

我会这样理解:

考虑一个渲染本机按钮DOM元素的FancyButton组件[DOM/叶子组件]

随着文章的进展,他们展示了如何在函数组件上转发引用:

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

想想看!文章的标题是“将引用转发给DOM组件”,然后在文章中讨论了“函数组件”,并提供了如何将引用转发给函数组件的示例。很可能他们将DOM组件视为简单的函数组件(类似叶子组件?)。所以我会这样解释这个注释:

引用转发不仅限于DOM组件**[简单的类似叶子的函数组件]。你也可以将引用转发给[复杂的非叶子的嵌套]**类组件实例。


<sup>
1: 在遗留文档中的那一点上,他们没有使用钩子,所以他们认为函数组件是无状态的。
</sup>

英文:

I am starting to think that actually from the context of Forwarding refs to dom components article, they use function components synonymously with DOM components.

Even furthermore, it sounds like they are thinking of leaf components as a subset of function components, although this is an ambiguous statement as they never define leaf components and you can create a leaf component based upon a class component as well. Any way in the ref forwarding article they mention:

> Although such encapsulation is desirable for application-level components like FeedStory or Comment, it can be inconvenient for highly reusable “leaf” components like FancyButton[certainly function component] or MyTextInput.

Also leaf components can have nested structures of pure dom elements, it's just they can't have further react components as child(ren)

Now the first line of the article itself is

> Consider a FancyButton component that renders the native button DOM element

Here FancyButton is a stateless<sup>1</sup> function component, .

I would take it as:

> Consider a FancyButton component[Dom/leaf component] that renders the native button DOM-element.

Then as the article progresses, they show how to forward ref on a function component as:

const FancyButton = React.forwardRef((props, ref) =&gt; (
  &lt;button ref={ref} className=&quot;FancyButton&quot;&gt;
    {props.children}
  &lt;/button&gt;
));

Come to think of it! The title of the article is Forwarding refs to DOM components and then in the article they talk about function components and give an example of how to forward a ref to a function component. Most likely they are taking DOM component as a simple function component(leaf like?). So I would interpret the note as following:

> ref forwarding is not limited to DOM components [simple leaf like function components]. You can forward refs to [complex non leaf like nested] class component instances, too.


<sup>
1: Up until that point in the legacy docs, they didn't have hooks, so they were considering functional components stateless.
</sup>

huangapple
  • 本文由 发表于 2023年6月1日 20:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定