mirror of
https://github.com/langgenius/dify.git
synced 2026-06-12 19:53:38 +08:00
refactor(web): use React use for context helper (#37289)
This commit is contained in:
parent
ba59d9a4ac
commit
7ec295fd66
@ -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'
|
||||
|
||||
@ -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,
|
||||
)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user