skip to Main Content

For debugging purposes, I often use console.log statements in React Native and Expo applications.

I tried to comment all the logs, but Suppose when we are working on Big Project it is not possible to comment all the console log.

2

Answers


  1. Chosen as BEST ANSWER

    Solutions: A babel plugin called babel-plugin-transform-remove-console takes care of removing any console statements from the code. This is a great plugin that I like to use, especially before releasing apps in production.

    yarn add -D babel-plugin-transform-remove-console
    

    module.exports = function () {
      return {
        env: {
          production: {
            plugins: ['transform-remove-console']
          }
        }
      };
    };


  2. The effective approach involves creating a shared function specifically for logging information, which will be executed in debug mode rather than release mode. Whenever you need to output something to the console, utilize this function instead of the default console.log. Below is the function I’m utilizing:

      export const showConsoleLogs = (key = null, value) => {
      try {
        if (__DEV__) {
          console.log(
            key == null ? "" : key + "=> ",
            value == null ? "Value Is Null" : JSON.stringify(value, null, 2)
          );
        }
      } catch (error) {
        console.log(error);
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search