Skip to content

Commit

Permalink
Add FallbackContext and FallbackProvider for handling suspense fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Dec 29, 2024
1 parent 8f280bf commit 28d7cd0
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions frontend/src/context/FallbackContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'

export type FallbackType = NonNullable<React.ReactNode> | null

export interface FallbackContextType {
updateFallback: (fallback: FallbackType) => void
}

// eslint-disable-next-line react-refresh/only-export-components
export const FallbackContext = React.createContext<FallbackContextType>({
updateFallback: () => { },
})

interface FabllbackProviderProps {
children: React.ReactNode
}

export const FabllbackProvider: React.FC<FabllbackProviderProps> = ({
children,
}) => {
const [fallback, setFallback] = React.useState<FallbackType>(null)

const updateFallback = React.useCallback((_fallback: FallbackType) => {
setFallback(() => _fallback)
}, [])

const renderChildren = React.useMemo(() => children, [children])

const value = React.useMemo(() => ({ updateFallback }), [updateFallback])

return (
<FallbackContext.Provider value={value}>
<React.Suspense fallback={fallback}>{renderChildren}</React.Suspense>
</FallbackContext.Provider>
)
}

0 comments on commit 28d7cd0

Please sign in to comment.