);
+ });
+ });
});
diff --git a/packages/react-server/src/ReactFizzHooks.js b/packages/react-server/src/ReactFizzHooks.js
index f6c8847a81b2d..6e77d5120d639 100644
--- a/packages/react-server/src/ReactFizzHooks.js
+++ b/packages/react-server/src/ReactFizzHooks.js
@@ -36,6 +36,7 @@ import {makeId} from './ReactServerFormatConfig';
import {
enableCache,
enableUseHook,
+ enableUseEventHook,
enableUseMemoCacheHook,
} from 'shared/ReactFeatureFlags';
import is from 'shared/objectIs';
@@ -502,6 +503,16 @@ export function useCallback(
return useMemo(() => callback, deps);
}
+function throwOnUseEventCall() {
+ throw new Error(
+ 'Cannot call a function returned by useEvent during server rendering.',
+ );
+}
+
+export function useEvent(callback: () => T): () => T {
+ return throwOnUseEventCall;
+}
+
// TODO Decide on how to implement this hook for server rendering.
// If a mutation occurs during render, consider triggering a Suspense boundary
// and falling back to client rendering.
@@ -657,6 +668,7 @@ export const Dispatcher: DispatcherType = {
useInsertionEffect: noop,
useLayoutEffect,
useCallback,
+ useEvent,
// useImperativeHandle is not run in the server environment
useImperativeHandle: noop,
// Effects are not run in the server environment.
@@ -675,6 +687,9 @@ if (enableCache) {
Dispatcher.getCacheForType = getCacheForType;
Dispatcher.useCacheRefresh = useCacheRefresh;
}
+if (enableUseEventHook) {
+ Dispatcher.useEvent = useEvent;
+}
if (enableUseMemoCacheHook) {
Dispatcher.useMemoCache = useMemoCache;
}
diff --git a/scripts/error-codes/codes.json b/scripts/error-codes/codes.json
index 4b50cbdcfcf44..605395dd44763 100644
--- a/scripts/error-codes/codes.json
+++ b/scripts/error-codes/codes.json
@@ -426,5 +426,6 @@
"438": "An unsupported type was passed to use(): %s",
"439": "We didn't expect to see a forward reference. This is a bug in the React Server.",
"440": "An event from useEvent was called during render.",
- "441": "An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error."
-}
+ "441": "An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.",
+ "442": "Cannot call a function returned by useEvent during server rendering."
+}
\ No newline at end of file