2022-08-03 18:21:20 +02:00
|
|
|
import React, { createContext, useContext } from 'react'
|
|
|
|
|
|
|
|
export type FlushEffectsHook = (callbacks: () => React.ReactNode) => void
|
|
|
|
|
|
|
|
export const FlushEffectsContext = createContext<FlushEffectsHook | null>(
|
|
|
|
null as any
|
|
|
|
)
|
|
|
|
|
2022-08-12 21:06:08 +02:00
|
|
|
export function useFlushEffects(callback: () => React.ReactNode): void {
|
|
|
|
const addFlushEffects = useContext(FlushEffectsContext)
|
2022-08-03 18:21:20 +02:00
|
|
|
// Should have no effects on client where there's no flush effects provider
|
2022-08-12 21:06:08 +02:00
|
|
|
if (addFlushEffects) {
|
|
|
|
addFlushEffects(callback)
|
|
|
|
}
|
2022-08-03 18:21:20 +02:00
|
|
|
}
|