A trick to silence annoying React warnings
6th Mar 2025
For years I've been developing with React and for years I've seen these annoying errors fill up my DevTools console:
I’m fine with having these errors, but why do they need to be so darn long?
But then I found this issue, and a wonderful workaround from dbalatero:
// Development React warnings in the console are really loud and it's hard to
// see the actual logs you care about.
//
// This patches `console.error` to detect and reformat the logs.
//
// This should not show up in production builds.
if (process.env.NODE_ENV === 'development') {
const consoleError = console.error;
// Add a hidden filter in the output so that we can easily filter out the log
// messages with the negative "-ReactDOMWarning" filter.
const hiddenFilter = 'ReactDOMWarning';
const hiddenStyles = 'color: transparent; font-size: 0px; user-select: none';
console.error = (...args: Parameters<typeof consoleError>) => {
// Fallback to normal console error unless this error came from react-dom.
const trace = new Error().stack;
if (!trace || !trace.includes('react-dom.development.js')) {
return consoleError(...args);
}
// All react-dom warnings start with "Warning:"
const firstArg = args[0];
if (typeof firstArg !== 'string' || !firstArg.startsWith('Warning:')) {
return consoleError(...args);
}
// If we get here, we're reasonably sure that it's a ReactDOM warning.
const template = args.shift()?.replace(/%s$/, '');
const stacktrace = args.pop();
console.groupCollapsed(
`%c⚠️ ${template}%c${hiddenFilter}`,
'color: red; font-weight: normal',
...args,
hiddenStyles
);
console.log(
`Tip: Add %c-${hiddenFilter}%c to the log Filter box to silence these ReactDOM error groups%c%{hiddenFilter}`,
'font-weight: bold',
'font-weight: normal',
hiddenStyles
);
consoleError(`%s%c${hiddenFilter}`, stacktrace, hiddenStyles);
console.groupEnd();
};
}
Result: A single line collapsible error. Wonderful! You can still press the toggle and see the whole thing, but it won't take up all the space.
This makes debugging easier and saves me so much time. I only wish I found this sooner!