diff --git a/.changeset/thick-rabbits-act.md b/.changeset/thick-rabbits-act.md new file mode 100644 index 000000000..f9fd5cfc9 --- /dev/null +++ b/.changeset/thick-rabbits-act.md @@ -0,0 +1,7 @@ +--- +"@suspensive/react-await": patch +"@suspensive/react-image": patch +"@suspensive/react": patch +--- + +refactor(react, react-await, react-image): replace createElement by jsx diff --git a/packages/react-await/src/Await.tsx b/packages/react-await/src/Await.tsx index 75aef9d89..f099a80d9 100644 --- a/packages/react-await/src/Await.tsx +++ b/packages/react-await/src/Await.tsx @@ -1,5 +1,5 @@ import type { FunctionComponent } from 'react' -import { createElement, useMemo } from 'react' +import { useMemo } from 'react' import { useSyncExternalStore } from 'use-sync-external-store/shim' import type { Tuple } from './types' import { hashKey } from './utils' @@ -55,8 +55,9 @@ export type AwaitProps = { /** * @experimental This is experimental feature. */ -export const Await = ({ children, options }: AwaitProps) => - createElement(children, useAwait(options)) +export const Await = ({ children: Children, options }: AwaitProps) => ( + (options)} /> +) class AwaitClient { private cache = new Map, AwaitState>() diff --git a/packages/react-image/src/Load.spec.ts b/packages/react-image/src/Load.spec.tsx similarity index 100% rename from packages/react-image/src/Load.spec.ts rename to packages/react-image/src/Load.spec.tsx diff --git a/packages/react-image/src/Load.ts b/packages/react-image/src/Load.tsx similarity index 93% rename from packages/react-image/src/Load.ts rename to packages/react-image/src/Load.tsx index 87d4d0661..bba6d3ec7 100644 --- a/packages/react-image/src/Load.ts +++ b/packages/react-image/src/Load.tsx @@ -1,4 +1,3 @@ -import { createElement } from 'react' import type { FunctionComponent } from 'react' import { useSyncExternalStore } from 'use-sync-external-store/shim' @@ -98,5 +97,6 @@ type LoadProps = { src: TLoadSrc children: FunctionComponent> } -export const Load = ({ src, children }: LoadProps) => - createElement(children, useLoad({ src })) +export const Load = ({ src, children: Children }: LoadProps) => ( + +) diff --git a/packages/react/src/ErrorBoundary.tsx b/packages/react/src/ErrorBoundary.tsx index f24b9b21e..f7a5aaa29 100644 --- a/packages/react/src/ErrorBoundary.tsx +++ b/packages/react/src/ErrorBoundary.tsx @@ -1,15 +1,5 @@ import type { ComponentProps, ComponentType, ErrorInfo, FunctionComponent, PropsWithChildren, ReactNode } from 'react' -import { - Component, - createContext, - createElement, - forwardRef, - useContext, - useImperativeHandle, - useMemo, - useRef, - useState, -} from 'react' +import { Component, createContext, forwardRef, useContext, useImperativeHandle, useMemo, useRef, useState } from 'react' import { ErrorBoundaryGroupContext } from './ErrorBoundaryGroup' import type { PropsWithoutChildren } from './types' import { assert, hasResetKeysChanged } from './utils' @@ -93,21 +83,18 @@ class BaseErrorBoundary extends Component + } else { + childrenOrFallback = fallback + } + } return ( - - {this.state.isError - ? typeof fallback === 'function' - ? createElement(fallback, { - error: this.state.error, - reset: this.reset, - }) - : fallback - : children} + + {childrenOrFallback} ) } diff --git a/packages/react/src/wrap.ts b/packages/react/src/wrap.tsx similarity index 84% rename from packages/react/src/wrap.ts rename to packages/react/src/wrap.tsx index 914426c59..03fd5bdff 100644 --- a/packages/react/src/wrap.ts +++ b/packages/react/src/wrap.tsx @@ -1,4 +1,3 @@ -import { createElement } from 'react' import type { ComponentProps, ComponentType } from 'react' import type { PropsWithoutChildren } from './types' import { Delay, ErrorBoundary, ErrorBoundaryGroup, Suspense } from '.' @@ -38,21 +37,23 @@ class WrapWithoutCSROnly { return this } - on = >(component: ComponentType) => { - const wrappedComponent = (props: TProps) => + on = >(Component: ComponentType) => { + const WrappedComponent = (props: TProps) => this.wrappers.reduce( - (acc, [wrapperComponent, wrapperProps]) => createElement(wrapperComponent as any, wrapperProps as any, acc), - createElement(component, props) + (acc, [WrapperComponent, wrapperProps]) => ( + {acc} + ), + ) if (process.env.NODE_ENV !== 'production') { - wrappedComponent.displayName = this.wrappers.reduce( - (acc, [wrapperComponent]) => `with${wrapperComponent.displayName}(${acc})`, - component.displayName || component.name || 'Component' + WrappedComponent.displayName = this.wrappers.reduce( + (acc, [WrapperComponent]) => `with${WrapperComponent.displayName}(${acc})`, + Component.displayName || Component.name || 'Component' ) } - return wrappedComponent + return WrappedComponent } }