refactor(web): use React use for context helper (#37289)

This commit is contained in:
lmlm 2026-06-11 19:44:00 +08:00 committed by GitHub
parent ba59d9a4ac
commit 7ec295fd66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 5 deletions

View File

@ -6,7 +6,7 @@ import { renderHook } from '@testing-library/react'
* and automatic error handling when context is used outside of its provider.
*
* Two variants are provided:
* - createCtx: Standard React context using useContext/createContext
* - createCtx: Standard React context using React's use/createContext
* - createSelectorCtx: Context with selector support using use-context-selector library
*/
import * as React from 'react'

View File

@ -1,9 +1,11 @@
import type { Context, Provider } from 'react'
import { createContext, useContext } from 'react'
import { createContext, use } from 'react'
import * as selector from 'use-context-selector'
type UseContextImpl = <T>(context: Context<T>) => T
const createCreateCtxFunction = (
useContextImpl: typeof useContext,
useContextImpl: UseContextImpl,
createContextImpl: typeof createContext,
) => {
return function<T>({ name, defaultValue }: CreateCtxOptions<T> = {}): CreateCtxReturn<T> {
@ -39,9 +41,9 @@ type CreateCtxReturn<T> = [Provider<T>, () => T, Context<T>] & {
// example
// const [AppProvider, useApp, AppContext] = createCtx<AppContextValue>()
export const createCtx = createCreateCtxFunction(useContext, createContext)
export const createCtx = createCreateCtxFunction(use, createContext)
export const createSelectorCtx = createCreateCtxFunction(
selector.useContext,
selector.useContext as UseContextImpl,
selector.createContext as typeof createContext,
)