Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/hooks/use-effect-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
//
// https://github.com/sanity-io/use-effect-event

import {useInsertionEffect, useRef} from 'react';
import {useInsertionEffect, useLayoutEffect, useRef} from 'react';

// useInsertionEffect was added in React 18; fall back to useLayoutEffect for
// React 16/17. Both run before useEffect, so ref.current is always up-to-date
// by the time any passive effect (or real event) reads it.
const useBeforeEffect = useInsertionEffect ?? useLayoutEffect;

function forbiddenInRender() {
throw new Error('useEffectEvent: invalid call during rendering.');
Expand All @@ -20,7 +25,7 @@ function useEffectEventPolyfill<const T extends (...args: any[]) => void>(
*/
const ref = useRef(forbiddenInRender as T);

useInsertionEffect(() => {
useBeforeEffect(() => {
ref.current = fn;
}, [fn]);

Expand Down