004917cac1
x-ref: [slack thread](https://vercel.slack.com/archives/C035J346QQL/p1664561699861189) Rename `useFlushEffects` to `useServerInsertedHTML`
20 lines
792 B
TypeScript
20 lines
792 B
TypeScript
import React, { useContext } from 'react'
|
|
|
|
export type ServerInsertedHTMLHook = (callbacks: () => React.ReactNode) => void
|
|
|
|
// Use `React.createContext` to avoid errors from the RSC checks because
|
|
// it can't be imported directly in Server Components:
|
|
//
|
|
// import { createContext } from 'react'
|
|
//
|
|
// More info: https://github.com/vercel/next.js/pull/40686
|
|
export const ServerInsertedHTMLContext =
|
|
React.createContext<ServerInsertedHTMLHook | null>(null as any)
|
|
|
|
export function useServerInsertedHTML(callback: () => React.ReactNode): void {
|
|
const addInsertedServerHTMLCallback = useContext(ServerInsertedHTMLContext)
|
|
// Should have no effects on client where there's no flush effects provider
|
|
if (addInsertedServerHTMLCallback) {
|
|
addInsertedServerHTMLCallback(callback)
|
|
}
|
|
}
|