gpt4 book ai didi

javascript - react 中的支持实例是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:49 24 4
gpt4 key购买 nike

尽管 React 文档中经常提到术语支持实例,但我无法理解它的含义。来自 react 文档:

To interact with the browser, you'll need a reference to a DOM node. You can attach a ref to any element, which allows you to reference the backing instance of the component. This is useful if you need to invoke imperative functions on the component, or want to access the underlying DOM nodes. link

This simplified component API is intended for components that are pure functions of their props. These components must not retain internal state, do not have backing instances, and do not have the component lifecycle methods. link

Because stateless functions don't have a backing instance, you can't attach a ref to a stateless function component. Normally this isn't an issue, since stateless functions do not provide an imperative API. Without an imperative API, there isn't much you could do with an instance anyway. However, if a user wants to find the DOM node of a stateless function component, they must wrap the component in a stateful component (eg. ES6 class component) and attach the ref to the stateful wrapper component.

最佳答案

后备实例是内存中代表节点的对象。这是通常存储状态等内容的地方。

因此,如果您有一个无状态组件,您将不会有一个支持实例,因为它完全是无状态的。

关于无状态组件的几点:

  • 没有生命周期方法
  • 没有引用

关于javascript - react 中的支持实例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37750207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com