> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/api/react/index.md.



# @lynx-js/react

欢迎来到 `@lynx-js/react` 的 API 参考文档，这个包提供了 ReactLynx 框架。如需了解 ReactLynx 的入门介绍，请访问 [ReactLynx 指南](/zh/react/index.md)。

## React APIs

ReactLynx 以 React 17 API 作为基础。你可以期待 `@lynx-js/react` 提供的 React APIs 与 React 17 或 [Preact](https://preactjs.com/guide/v10/switching-to-preact/) 完全兼容。

以下是 ReactLynx 提供的 API 一览。如无特殊说明，这些 API 将和 [React 官方参考](https://react.dev/reference/react) 行为一致，否则我们将重点描述其在 Lynx 平台中的注意事项。

:::details 为什么是以兼容 React 17 为基础？
React 从 [React 18](https://react.dev/blog/2022/03/29/react-v18) 开始正式进入「并发」时代。以 `useTransition`、`useDeferredValue` 这些 API 为代表的「并发 React 功能」对执行环境的调度方式和并发模型有着非常苛刻的要求 —— 包括依赖或覆写浏览器 Web API、依赖 React Native 新架构等。Lynx 在架构演进上有自己的特点和思考，因此我们选择了 React 17 作为 ReactLynx 的基石，在保留 React 编程模型的前提下，向 Lynx 体系演进。

于此同时，我们也会继续关注 [React 19](https://react.dev/blog/2024/04/25/react-19) 与 [React Compiler](https://react.dev/learn/react-compiler) 等后续 React 演进中不依赖并发架构的部分，并尝试将其整合进 ReactLynx 体系中。
:::

## 文档

| 文档                                                           | 描述 |
| ------------------------------------------------------------ | -- |
| [built-in-macros](/zh/api/react/Document.built-in-macros.md) | -  |
| [directives](/zh/api/react/Document.directives.md)           | -  |

## 类

| Class                                                 | 描述                                                                                                                                                                                                                                                                |
| ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Component](/zh/api/react/Class.Component.md)         | -                                                                                                                                                                                                                                                                 |
| [MainThreadRef](/zh/api/react/Class.MainThreadRef.md) | A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve states between main thread function calls. The data saved in `current` property of the `MainThreadRef` can be read and written in multiple main thread functions. |
| [PureComponent](/zh/api/react/Class.PureComponent.md) | -                                                                                                                                                                                                                                                                 |

## 接口

| Interface                                                                     | 描述                                                                                                                              |
| ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| [DataProcessorDefinition](/zh/api/react/Interface.DataProcessorDefinition.md) | Definition of DataProcessor(s)                                                                                                  |
| [DataProcessors](/zh/api/react/Interface.DataProcessors.md)                   | The data processors that registered with [Lynx.registerDataProcessors](/zh/api/react/Interface.Lynx.md#registerdataprocessors). |
| [GlobalProps](/zh/api/react/Interface.GlobalProps.md)                         | The interface you can extends so that the `useGlobalProps` returning value can be customized                                    |
| [InitData](/zh/api/react/Interface.InitData.md)                               | The interface you can extends so that the `defaultDataProcessor` returning value can be customized                              |
| [InitDataRaw](/zh/api/react/Interface.InitDataRaw.md)                         | The interface you can extends so that the `defaultDataProcessor` parameter can be customized                                    |
| [Lynx](/zh/api/react/Interface.Lynx.md)                                       | APIs under `lynx` global variable that added by ReactLynx.                                                                      |
| [Root](/zh/api/react/Interface.Root.md)                                       | The default root exported by `@lynx-js/react` for you to render a JSX                                                           |

## 变量

| Variable                                                       | 描述                                                           |
| -------------------------------------------------------------- | ------------------------------------------------------------ |
| [root](/zh/api/react/Variable.root.md)                         | The default and only root of ReactLynx for you to render JSX |
| [useErrorBoundary](/zh/api/react/Variable.useErrorBoundary.md) | -                                                            |

## 函数

| Function                                                                           | 描述                                                                                                                                                                                               |
| ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [cloneElement](/zh/api/react/Function.cloneElement.md)                             | -                                                                                                                                                                                                |
| [createContext](/zh/api/react/Function.createContext.md)                           | Lets you create a Context that components can provide or read.                                                                                                                                   |
| [createElement](/zh/api/react/Function.createElement.md)                           | -                                                                                                                                                                                                |
| [createPortal](/zh/api/react/Function.createPortal.md)                             | 将 `vnode` 渲染到 `container` 中，而不是当前组件在元素树中的位置，同时让它仍然保留在 React 树中。                                                                                                                                  |
| [createRef](/zh/api/react/Function.createRef.md)                                   | -                                                                                                                                                                                                |
| [forwardRef](/zh/api/react/Function.forwardRef.md)                                 | Lets your component expose a DOM node to a parent component using a ref.                                                                                                                         |
| [Fragment](/zh/api/react/Function.Fragment.md)                                     | Lets you group elements without a wrapper node.                                                                                                                                                  |
| [isValidElement](/zh/api/react/Function.isValidElement.md)                         | -                                                                                                                                                                                                |
| [lazy](/zh/api/react/Function.lazy.md)                                             | Lets you defer loading a component’s code until it is rendered for the first time.                                                                                                               |
| [memo](/zh/api/react/Function.memo.md)                                             | Lets you skip re-rendering a component when its props are unchanged.                                                                                                                             |
| [runOnBackground](/zh/api/react/Function.runOnBackground.md)                       | `runOnBackground` allows triggering js functions on the background thread asynchronously.                                                                                                        |
| [runOnMainThread](/zh/api/react/Function.runOnMainThread.md)                       | `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.                                                                                                     |
| [Suspense](/zh/api/react/Function.Suspense.md)                                     | Lets you display a fallback until its children have finished loading.                                                                                                                            |
| [useCallback](/zh/api/react/Function.useCallback.md)                               | `useCallback` will return a memoized version of the callback that only changes if one of the `inputs` has changed.                                                                               |
| [useContext](/zh/api/react/Function.useContext.md)                                 | Accepts a context object (the value returned from `React.createContext`) and returns the current context value, as given by the nearest context provider for the given context.                  |
| [useDebugValue](/zh/api/react/Function.useDebugValue.md)                           | `useDebugValue` can be used to display a label for custom hooks in React DevTools.                                                                                                               |
| [useEffect](/zh/api/react/Function.useEffect.md)                                   | Accepts a function that contains imperative, possibly effectful code. The effects run after main thread dom update without blocking it.                                                          |
| [useGlobalProps](/zh/api/react/Function.useGlobalProps.md)                         | A React Hooks for you to get `lynx.__globalProps`. If `lynx.__globalProps` is changed, a re-render will be triggered automatically.                                                              |
| [useGlobalPropsChanged](/zh/api/react/Function.useGlobalPropsChanged.md)           | A React Hooks for you to get notified when `__globalProps` changed.                                                                                                                              |
| [useImperativeHandle](/zh/api/react/Function.useImperativeHandle.md)               | `useImperativeHandle` customizes the instance value that is exposed to parent components when using `ref`. As always, imperative code using refs should be avoided in most cases.                |
| [useInitData](/zh/api/react/Function.useInitData.md)                               | A React Hooks for you to get `initData`. If `initData` is changed, a re-render will be triggered automatically.                                                                                  |
| [useInitDataChanged](/zh/api/react/Function.useInitDataChanged.md)                 | A React Hooks for you to get notified when `initData` changed.                                                                                                                                   |
| [useLayoutEffect](/zh/api/react/Function.useLayoutEffect.md)                       | `useLayoutEffect` is now an alias of `useEffect`. Use `useEffect` instead.                                                                                                                       |
| [useLynxGlobalEventListener](/zh/api/react/Function.useLynxGlobalEventListener.md) | `useLynxGlobalEventListener` helps you `addListener` as early as possible.                                                                                                                       |
| [useMainThreadRef](/zh/api/react/Function.useMainThreadRef.md)                     | Create A `MainThreadRef`.                                                                                                                                                                        |
| [useMemo](/zh/api/react/Function.useMemo.md)                                       | `useMemo` will only recompute the memoized value when one of the `deps` has changed.                                                                                                             |
| [useReducer](/zh/api/react/Function.useReducer.md)                                 | An alternative to `useState`.                                                                                                                                                                    |
| [useRef](/zh/api/react/Function.useRef.md)                                         | `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument (`initialValue`). The returned object will persist for the full lifetime of the component. |
| [useState](/zh/api/react/Function.useState.md)                                     | Returns a stateful value, and a function to update it.                                                                                                                                           |
| [useSyncExternalStore](/zh/api/react/Function.useSyncExternalStore.md)             | -                                                                                                                                                                                                |
| [withInitDataInState](/zh/api/react/Function.withInitDataInState.md)               | Higher-Order Component (HOC) that injects `initData` into the state of the given class component.                                                                                                |

## Components

| Function                                                             | 描述                                                                                                                                                                                                                  |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [GlobalPropsConsumer](/zh/api/react/Function.GlobalPropsConsumer.md) | The [Consumer](https://react.dev/reference/react/createContext#consumer) Component that provide `lynx.__globalProps`. This should be used with [GlobalPropsProvider](/zh/api/react/Function.GlobalPropsProvider.md) |
| [GlobalPropsProvider](/zh/api/react/Function.GlobalPropsProvider.md) | The [Provider](https://react.dev/reference/react/createContext#provider) Component that provide `lynx.__globalProps`, you must wrap your JSX inside it                                                              |
| [InitDataConsumer](/zh/api/react/Function.InitDataConsumer.md)       | The [Consumer](https://react.dev/reference/react/createContext#consumer) Component that provide `initData`. This should be used with [InitDataProvider](/zh/api/react/Function.InitDataProvider.md)                 |
| [InitDataProvider](/zh/api/react/Function.InitDataProvider.md)       | The [Provider](https://react.dev/reference/react/createContext#provider) Component that provide `initData`, you must wrap your JSX inside it                                                                        |
