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

标签 javascript reactjs

尽管 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/

相关文章:

node.js - 401 post 请求错误 Stormpath Express + React + Node + Gulp

reactjs - 如何在ES6 +的webpack的node_modules上使用babel loader?

reactjs - 使用react-router设置初始路由

javascript - 0.59.9版本的React Native项目创建时报错: couldn't find template. config.js

javascript - 在动态创建的按钮内调用函数

JavaScript:在方法中使用对象变量

javascript - modernizr 如何检查对 webforms2.0 中新输入属性的支持?

javascript - FullCalendar jQuery 插件 - 设置今天的日期

javascript - 在 Azure 应用服务上使用 Node 和 Express 进行路由

javascript - mapStateToProps 将数据传递给 Prop