-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add experimental wrap to remove unnecessary hocs
- Loading branch information
Showing
10 changed files
with
107 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type ComponentProps, type ComponentType, createElement } from 'react' | ||
import type { PropsWithoutChildren } from './types' | ||
import type { AsyncBoundaryProps, DelayProps, ErrorBoundaryGroupProps, ErrorBoundaryProps, SuspenseProps } from '.' | ||
|
||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
export const wrap = | ||
< | ||
TWrapperProps extends SuspenseProps | ErrorBoundaryProps | ErrorBoundaryGroupProps | DelayProps | AsyncBoundaryProps | ||
>( | ||
wrapper: ComponentType<TWrapperProps>, | ||
wrapperProps: PropsWithoutChildren<TWrapperProps> | ||
) => | ||
<TProps extends ComponentProps<ComponentType>>(component: ComponentType<TProps>) => { | ||
const wrapped = (props: TProps) => | ||
createElement(wrapper, wrapperProps as TWrapperProps, createElement(component, props)) | ||
if (process.env.NODE_ENV !== 'production') { | ||
const name = component.displayName || component.name || 'Component' | ||
wrapped.displayName = `with${wrapper.displayName}(${name})` | ||
} | ||
return wrapped | ||
} |
28 changes: 28 additions & 0 deletions
28
websites/visualization/src/app/react/experimental/wrap/after/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use client' | ||
import { useErrorBoundary, withErrorBoundary, withErrorBoundaryGroup, withSuspense } from '@suspensive/react' | ||
import { UseSuspenseQuery } from '~/components' | ||
import { api } from '~/utils' | ||
|
||
const logError = (error: Error) => console.error(error) | ||
|
||
export default withErrorBoundaryGroup( | ||
withErrorBoundary( | ||
withSuspense( | ||
() => { | ||
const errorBoundary = useErrorBoundary() | ||
|
||
return ( | ||
<> | ||
<button onClick={() => errorBoundary.setError(new Error('trigger error by useErrorBoundary().setError'))}> | ||
trigger error by useErrorBoundary().setError | ||
</button> | ||
<UseSuspenseQuery queryKey={['wrap', 1] as const} queryFn={() => api.delay(200, { percentage: 50 })} /> | ||
</> | ||
) | ||
}, | ||
{ fallback: <>loading...</> } | ||
), | ||
{ fallback: (props) => <>{props.error.message}</>, onError: logError } | ||
), | ||
{ blockOutside: false } | ||
) |
23 changes: 23 additions & 0 deletions
23
websites/visualization/src/app/react/experimental/wrap/before/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client' | ||
import { ErrorBoundary, ErrorBoundaryGroup, Suspense, useErrorBoundary, wrap } from '@suspensive/react' | ||
import { UseSuspenseQuery } from '~/components' | ||
import { api } from '~/utils' | ||
|
||
const logError = (error: Error) => console.error(error) | ||
|
||
export default wrap(ErrorBoundaryGroup, { blockOutside: false })( | ||
wrap(ErrorBoundary, { fallback: (props) => <>{props.error.message}</>, onError: logError })( | ||
wrap(Suspense, { fallback: <>loading...</> })(() => { | ||
const errorBoundary = useErrorBoundary() | ||
|
||
return ( | ||
<> | ||
<button onClick={() => errorBoundary.setError(new Error('trigger error by useErrorBoundary().setError'))}> | ||
trigger error by useErrorBoundary().setError | ||
</button> | ||
<UseSuspenseQuery queryKey={['with', 1] as const} queryFn={() => api.delay(200, { percentage: 50 })} /> | ||
</> | ||
) | ||
}) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters