> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Function: useMainThreadRef()

## Call Signature

> **useMainThreadRef**\<`T`>(`initValue`: `T`): [`MainThreadRef`](/next/zh/api/react/Types/MainThreadRef.md)\<`T`>

Create A `MainThreadRef`.

`MainThreadRef` 是一种只能在主线程访问的 ref，用于在主线程函数调用之间保存状态。保存在 `MainThreadRef` 的 `current` 属性中的数据，可以在多个主线程函数中读写。

它是一个 Hook，只应在组件的顶层调用。

### Type Parameters

| Type Parameter |
| -------------- |
| `T`            |

### Parameters

| Parameter   | Type | Description           |
| ----------- | ---- | --------------------- |
| `initValue` | `T`  | `MainThreadRef` 的初始值。 |

### Returns

[`MainThreadRef`](/next/zh/api/react/Types/MainThreadRef.md)\<`T`>

### Example

```ts
import { useMainThreadRef } from '@lynx-js/react'
import type { MainThread } from '@lynx-js/types'

export function App() {
  const ref = useMainThreadRef<MainThread.Element>(null)

  const handleTap = () => {
    'main thread'
    ref.current?.setStyleProperty('background-color', 'blue')
  }

  return (
    <view
      main-thread:ref={ref}
      main-thread:bindtap={handleTap}
      style={{ width: '300px', height: '300px' }}
    />
  )
}
```

## Call Signature

> **useMainThreadRef**\<`T`>(`initValue`: `T` | `null`): `RefObject`\<`T`>

Create A `MainThreadRef`.

`MainThreadRef` 是一种只能在主线程访问的 ref，用于在主线程函数调用之间保存状态。保存在 `MainThreadRef` 的 `current` 属性中的数据，可以在多个主线程函数中读写。

它是一个 Hook，只应在组件的顶层调用。

### Type Parameters

| Type Parameter |
| -------------- |
| `T`            |

### Parameters

| Parameter   | Type          | Description           |
| ----------- | ------------- | --------------------- |
| `initValue` | `T` \| `null` | `MainThreadRef` 的初始值。 |

### Returns

`RefObject`\<`T`>

### Example

```ts
import { useMainThreadRef } from '@lynx-js/react'
import type { MainThread } from '@lynx-js/types'

export function App() {
  const ref = useMainThreadRef<MainThread.Element>(null)

  const handleTap = () => {
    'main thread'
    ref.current?.setStyleProperty('background-color', 'blue')
  }

  return (
    <view
      main-thread:ref={ref}
      main-thread:bindtap={handleTap}
      style={{ width: '300px', height: '300px' }}
    />
  )
}
```

## Call Signature

> **useMainThreadRef**\<`T` = `undefined`>(): [`MainThreadRef`](/next/zh/api/react/Types/MainThreadRef.md)\<`T` | `undefined`>

Create A `MainThreadRef`.

`MainThreadRef` 是一种只能在主线程访问的 ref，用于在主线程函数调用之间保存状态。保存在 `MainThreadRef` 的 `current` 属性中的数据，可以在多个主线程函数中读写。

它是一个 Hook，只应在组件的顶层调用。

### Type Parameters

| Type Parameter | Default type |
| -------------- | ------------ |
| `T`            | `undefined`  |

### Returns

[`MainThreadRef`](/next/zh/api/react/Types/MainThreadRef.md)\<`T` | `undefined`>

### Example

```ts
import { useMainThreadRef } from '@lynx-js/react'
import type { MainThread } from '@lynx-js/types'

export function App() {
  const ref = useMainThreadRef<MainThread.Element>(null)

  const handleTap = () => {
    'main thread'
    ref.current?.setStyleProperty('background-color', 'blue')
  }

  return (
    <view
      main-thread:ref={ref}
      main-thread:bindtap={handleTap}
      style={{ width: '300px', height: '300px' }}
    />
  )
}
```
