admin管理员组

文章数量:1287646

import { createContext, ReactNode } from "react"

interface CascaderProps<T> {
  getLabel: (item: T) => string
}

export const createCascaderContext = <T,>(
  defaultValue: CascaderProps<T> | null
) => {
  return createContext(defaultValue)
}

const CascaderContext = createCascaderContext(null)

export const CascaderProvider = <T,>(
  context: CascaderProps<T>,
  children?: ReactNode
) => {
  return (
    <CascaderContext.Provider value={context}>
      {children}
    </CascaderContext.Provider>
  )
}

export default CascaderContext

I can't use generic T, ts infers T as unknown, what is the correct way to do it?


2025-2-24 update

Maybe I misunderstood, I think this is caused by the lack of support for Typescript in the React library. If you think this question is the same as the previous one I asked, please explain it in detail.

2025-2-24 update

I checked this answer but it uses any and it doesn't make sense to solve the problem like that.

本文标签: reactjsReact ContextProvider Generics T are not acceptedStack Overflow