Skip to content

Commit e98d94a

Browse files
authored
Merge pull request #58 from vikejs/nitedani/dev
Rename withFallback options
2 parents 3d3b29f + 251b5ac commit e98d94a

3 files changed

Lines changed: 25 additions & 25 deletions

File tree

examples/react-query/pages/index/@id/+Page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const Movie = withFallback(
4242
)
4343
},
4444
{
45-
Fallback: ({ id }) => `Loading movie ${id}`,
45+
Loading: ({ id }) => `Loading movie ${id}`,
4646
// Try commenting out the error fallback
47-
FallbackError: ({ id, error, retry }) => (
47+
Error: ({ id, error, retry }) => (
4848
<>
4949
<div>Loading movie {id} failed</div>
5050
<div>{error.message}</div>

packages/vike-react-query/src/withFallback.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const ComponentThatThrows = withFallback(
4646
throw new Error('some message')
4747
},
4848
{
49-
Fallback: ({ count }) => `loading${count}`,
50-
FallbackError: ({ count, error, onErrorFallbackMount }) => {
49+
Loading: ({ count }) => `loading${count}`,
50+
Error: ({ count, error, onErrorFallbackMount }) => {
5151
useEffect(() => {
5252
onErrorFallbackMount?.()
5353
}, [])

packages/vike-react-query/src/withFallback.tsx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ type ErrorFallbackProps = {
1010
retry: RetryFn
1111
}
1212

13-
type Fallback<T> = ComponentType<T> | ReactNode
14-
type FallbackError<T> = ComponentType<T & ErrorFallbackProps> | ReactNode
13+
type Loading<T> = ComponentType<T> | ReactNode
14+
type Error<T> = ComponentType<T & ErrorFallbackProps> | ReactNode
1515

1616
type WithFallbackOptions<T> = {
17-
Fallback?: Fallback<T>
18-
FallbackError?: FallbackError<T>
17+
Loading?: Loading<T>
18+
Error?: Error<T>
1919
}
2020

2121
export function withFallback<T extends object = Record<string, never>>(
@@ -24,29 +24,29 @@ export function withFallback<T extends object = Record<string, never>>(
2424
): ComponentType<T>
2525
export function withFallback<T extends object = Record<string, never>>(
2626
Component: ComponentType<T>,
27-
Fallback?: Fallback<T>,
28-
FallbackError?: FallbackError<T>
27+
Loading?: Loading<T>,
28+
Error?: Error<T>
2929
): ComponentType<T>
3030
export function withFallback<T extends object = Record<string, never>>(
3131
Component: ComponentType<T>,
32-
options?: Fallback<T> | WithFallbackOptions<T>,
33-
FallbackError_?: FallbackError<T>
32+
options?: Loading<T> | WithFallbackOptions<T>,
33+
Error_?: Error<T>
3434
): ComponentType<T> {
35-
let Fallback: Fallback<T>
36-
let FallbackError: ComponentType<T & ErrorFallbackProps> | ReactNode
35+
let Loading: Loading<T>
36+
let Error: Error<T>
3737

38-
if (options && typeof options === 'object' && ('Fallback' in options || 'FallbackError' in options)) {
39-
Fallback = options.Fallback
40-
FallbackError = options.FallbackError
38+
if (options && typeof options === 'object' && ('Loading' in options || 'Error' in options)) {
39+
Loading = options.Loading
40+
Error = options.Error
4141
} else if (options && typeof options !== 'object') {
42-
Fallback = options
43-
FallbackError = FallbackError_
42+
Loading = options
43+
Error = Error_
4444
}
4545

4646
const ComponentWithFallback = (componentProps: T) => {
47-
if (FallbackError) {
47+
if (Error) {
4848
return (
49-
<Suspense fallback={typeof Fallback === 'function' ? <Fallback {...componentProps} /> : Fallback}>
49+
<Suspense fallback={typeof Loading === 'function' ? <Loading {...componentProps} /> : Loading}>
5050
<QueryErrorResetBoundary>
5151
{({ reset }) => {
5252
const createRetry =
@@ -70,14 +70,14 @@ export function withFallback<T extends object = Record<string, never>>(
7070
return (
7171
<ErrorBoundary
7272
fallbackRender={({ error: originalError, resetErrorBoundary }) =>
73-
typeof FallbackError === 'function' ? (
74-
<FallbackError
73+
typeof Error === 'function' ? (
74+
<Error
7575
{...componentProps}
7676
retry={createRetry(resetErrorBoundary)}
7777
error={createError(originalError)}
7878
/>
7979
) : (
80-
FallbackError
80+
Error
8181
)
8282
}
8383
>
@@ -91,7 +91,7 @@ export function withFallback<T extends object = Record<string, never>>(
9191
}
9292

9393
return (
94-
<Suspense fallback={typeof Fallback === 'function' ? <Fallback {...componentProps} /> : Fallback}>
94+
<Suspense fallback={typeof Loading === 'function' ? <Loading {...componentProps} /> : Loading}>
9595
<Component {...componentProps} />
9696
</Suspense>
9797
)

0 commit comments

Comments
 (0)