Skip to content

Commit

Permalink
Return function that returns boolean for useRenderGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Nov 28, 2023
1 parent 8101e02 commit ea821a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
14 changes: 4 additions & 10 deletions src/react/hooks/internal/useRenderGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ https://github.com/facebook/relay/blob/8651fbca19adbfbb79af7a3bc40834d105fd7747/
export function useRenderGuard() {
RenderDispatcher = getRenderDispatcher();

// We use a callback argument here instead of the failure string so that the
// call site can provide a custom failure message while allowing for static
// message extraction on the `invariant` function.
return React.useCallback((onFailure: () => void) => {
if (
RenderDispatcher !== null &&
RenderDispatcher === getRenderDispatcher()
) {
onFailure();
}
return React.useCallback(() => {
return (
RenderDispatcher !== null && RenderDispatcher === getRenderDispatcher()
);
}, []);
}
14 changes: 6 additions & 8 deletions src/react/hooks/useLoadableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function useLoadableQuery<
queryRef.promiseCache = promiseCache;
}

const failDuringRender = useRenderGuard();
const calledDuringRender = useRenderGuard();

React.useEffect(() => queryRef?.retain(), [queryRef]);

Expand Down Expand Up @@ -183,12 +183,10 @@ export function useLoadableQuery<

const loadQuery: LoadQueryFunction<TVariables> = React.useCallback(
(...args) => {
failDuringRender(() => {
invariant(
false,
"useLoadableQuery: 'loadQuery' should not be called during render. To start a query during render, use the 'useBackgroundQuery' hook."
);
});
invariant(
!calledDuringRender(),
"useLoadableQuery: 'loadQuery' should not be called during render. To start a query during render, use the 'useBackgroundQuery' hook."
);

const [variables] = args;

Expand All @@ -214,7 +212,7 @@ export function useLoadableQuery<
suspenseCache,
watchQueryOptions,
promiseCache,
failDuringRender,
calledDuringRender,
]
);

Expand Down

0 comments on commit ea821a9

Please sign in to comment.