From 9e0e509540923f0808c85cac9d6b841708918d7c Mon Sep 17 00:00:00 2001 From: bendtherules Date: Sun, 15 Dec 2019 13:18:21 +0530 Subject: [PATCH] Add renderer helper - types and is check functions --- src/helpers.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/helpers.ts diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000..919cef7 --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,40 @@ +import { + ElementType, + FunctionComponent, + ComponentClass, + PropsWithChildren, + ReactText +} from "react"; + +export type ReactElementSubset = { + props: PropsWithChildren; + type: ElementType; +}; + +export type ReactElementSubsetWithPrimitive = + | ReactElementSubset + | ReactText + | Array + | null + | undefined; + +export function isFunctionalComponent( + eleType: ElementType +): eleType is FunctionComponent { + return ( + typeof eleType === "function" && // can be various things + !( + (eleType.prototype && eleType.prototype.isReactComponent) // native arrows don't have prototypes // special property + ) + ); +} + +export function isClassComponent( + eleType: ElementType +): eleType is ComponentClass { + return !!( + typeof eleType === "function" && + eleType.prototype && + eleType.prototype.isReactComponent + ); +}