9

I have an app made with react-native CLI, my version of RN is 0.63, I'm getting annoying moment.js warning like bellow:

Require cycle: node_modules\moment\src\lib\create\local.js -> node_modules\moment\src\lib\create\from-anything.js -> node_modules\moment\src\lib\create\from-string-and-array.js -> node_modules\moment\src\lib\create\from-string-and-format.js -> node_modules\moment\src\lib\create\from-array.js -> node_modules\moment\src\lib\create\local.js

I'm trying to make it so it will ignore this warning by using LogBox but it's not working.

I'm doing this in my App.js file:

import { LogBox  } from 'react-native';
LogBox.ignoreLogs(['Require cycle:']);

Any idea how can I make it so react-native will ignore the warnings?

gabogabans
  • 3,035
  • 5
  • 33
  • 81

3 Answers3

0

Be careful where the error occurs, more precisely in which screen because the warning may appear after you have ignored it.

I don't understand why it's not global, but for me that was the problem.

Adrian
  • 17
  • 2
0

In index.js or App.js just set:

import { LogBox } from "react-native"

LogBox.ignoreAllLogs(true)

or

import { YellowBox } from "react-native"

YellowBox.ignoreWarnings(["Warning: ..."])
Deepak Singh
  • 749
  • 4
  • 16
0

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!

M Muller
  • 157
  • 1
  • 4