Files
payloadcms/packages/payload/src/utilities/isReactComponent.ts
Alessio Gravili 1cf7d4db32 feat!: support latest Next.js versions, fix server-only props being passed to Client Document Views (#7026)
**BREAKING:** The minimum required Next.js version has been bumped from
`15.0.0-rc.0` to `15.0.0-canary.53`. This is because the way client
components are represented changed somewhere between those versions, and
it is not feasible to support both versions in our RSC detection logic.
2024-07-08 09:24:56 -04:00

36 lines
1.2 KiB
TypeScript

import type React from 'react'
const clientRefSymbol = Symbol.for('react.client.reference')
export function isReactServerComponentOrFunction<T extends any>(
component: React.ComponentType | any,
): component is T {
if (component === null || component === undefined) {
return false
}
const hasClientComponentSymbol = component.$$typeof == clientRefSymbol
const isFunctionalComponent = typeof component === 'function'
// Anonymous functions are Client Components in Turbopack. RSCs should have a name
const isAnonymousFunction = typeof component === 'function' && component.name === ''
const isRSC = isFunctionalComponent && !isAnonymousFunction && !hasClientComponentSymbol
return isRSC
}
export function isReactClientComponent<T extends any>(
component: React.ComponentType | any,
): component is T {
if (component === null || component === undefined) {
return false
}
return !isReactServerComponentOrFunction(component) && component.$$typeof == clientRefSymbol
}
export function isReactComponentOrFunction<T extends any>(
component: React.ComponentType | any,
): component is T {
return isReactServerComponentOrFunction(component) || isReactClientComponent(component)
}