The strong need to solve the problem led me to implement the following unconventional alternative but its usefulness is worth it. First, it makes a modification to the console.warn
method, although without risk, because it does so by injecting a necessary functionality without modifying its original behavior). Then it takes advantage of this modification from a LogBox replica of the original but with the necessary modifications so that it performs the exclusion.
The implementation:
import { LogBox as BaseLogBox } from 'react-native';
// aux variable to store exceptions
let warnExceptions = [];
// aux variable to store original function
const console_warn = console.warn;
// overrides the console.warn method keeping its functionality
// intact but adding the possibility of exclusions
console.warn = (...args) => {
const isException = warnExceptions
.some(ex => args[0]?.startsWith(ex));
if (isException) return;
// keeps original functionality
console_warn(...args);
}
// override the LogBox methods to add the exclusions
export let LogBox = { ...BaseLogBox };
LogBox.ignoreLogs = (messages) => {
warnExceptions = [...warnExceptions, ...messages];
// keeps original functionality
BaseLogBox.ignoreLogs(messages);
}
LogBox.ignoreAllLogs = () => {
// this is because all strings start with ''
warnExceptions = [''];
// keeps original functionality
BaseLogBox.ignoreAllLogs();
}
We then use this new LogBox instead of the original:
import { LogBox } from 'path-of-new-logbox';
// ...
LogBox.ignoreLogs(['Require cycle:']);
Obs: I didn't really look into whether the original .ignoreLogs()
method adds or replaces the messages passed by parameter each time it is called, or anything else. The important thing was to fix the problem to the point where it's useful, without going much deeper than necessary. Any necessary variation in any case can be done without major problems, it is in each one. I hope you find it useful!