Skip to content

Commit

Permalink
useWarningLogger only runs on the client (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
r100-stack authored Sep 19, 2024
1 parent a1a9154 commit ba9bc9a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/itwinui-react/src/utils/hooks/useWarningLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as React from 'react';
import { isUnitTest } from '../functions/dev.js';
import { getWindow } from '../functions/dom.js';

const _React = React as any;
const ReactInternals =
Expand All @@ -28,7 +29,7 @@ export const useWarningLogger =
process.env.NODE_ENV === 'development' && !isUnitTest
? function () {
const loggedRef = React.useRef(false);
const timeoutRef = React.useRef<number | null>(null);
const timeoutRef = React.useRef<number | undefined>(undefined);

// https://stackoverflow.com/a/71685253
const stack =
Expand All @@ -44,7 +45,7 @@ export const useWarningLogger =
const logWarning = React.useCallback(
(message: string) => {
// Using setTimeout to delay execution until after rendering is complete.
timeoutRef.current = window.setTimeout(() => {
timeoutRef.current = getWindow()?.setTimeout(() => {
if (!loggedRef.current) {
console.error(prefix, message);
loggedRef.current = true;
Expand All @@ -59,7 +60,7 @@ export const useWarningLogger =
// The warning should be logged only once per component instance.
return () => {
if (timeoutRef.current) {
window.clearTimeout(timeoutRef.current);
getWindow()?.clearTimeout(timeoutRef.current);
}
};
}, []);
Expand Down

0 comments on commit ba9bc9a

Please sign in to comment.