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, and this page is available as Markdown at /next/zh/api/react/Hooks/useMainThreadRef.md.
  • 简体中文
  • 函数: useMainThreadRef()

    调用签名

    useMainThreadRef<T>(initValue: T): MainThreadRef<T>

    Create A MainThreadRef.

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

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

    类型参数

    类型参数
    T

    参数

    参数类型描述
    initValueTMainThreadRef 的初始值。

    返回

    MainThreadRef<T>

    示例

    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' }}
        />
      )
    }

    调用签名

    useMainThreadRef<T>(initValue: T | null): RefObject<T>

    Create A MainThreadRef.

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

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

    类型参数

    类型参数
    T

    参数

    参数类型描述
    initValueT | nullMainThreadRef 的初始值。

    返回

    RefObject<T>

    示例

    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' }}
        />
      )
    }

    调用签名

    useMainThreadRef<T = undefined>(): MainThreadRef<T | undefined>

    Create A MainThreadRef.

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

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

    类型参数

    类型参数默认类型
    Tundefined

    返回

    MainThreadRef<T | undefined>

    示例

    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' }}
        />
      )
    }
    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。