Câu hỏi phỏng vấn React Native
Câu hỏi

How do you perform logging in React native?

Câu trả lời

Logging in React Native can be performed using several methods, each with its own advantages and use cases. Here are the primary ways to perform logging in React Native:

1. Using Built-in Console Methods

The simplest way to perform logging in React Native is by using the built-in console methods such as console.log, console.warn, and console.error. These methods are straightforward and useful for quick debugging during development.

  • console.log: Logs general information.
    console.log("This is a log message");
  • console.warn: Logs warnings, typically displayed in a yellow box.
    console.warn("This is a warning message");
  • console.error: Logs errors, typically displayed in a red box.
    console.error("This is an error message");

2. Using React Native CLI Commands

For viewing logs in the terminal, you can use the React Native CLI commands:

  • For iOS:
    npx react-native log-ios
  • For Android:
    npx react-native log-android

These commands will display all logs, including those from console.log, console.warn, and console.error, in the terminal where the app is running[3][6].

3. Using react-native-logs Library

For more advanced logging, you can use the react-native-logs library, which provides additional features such as custom log levels, colored logs, and multiple transports (e.g., console, file, remote server).

  • Installation:

    npm install --save react-native-logs
    # or
    yarn add react-native-logs
  • Basic Usage:

    import { logger } from 'react-native-logs';
    const log = logger.createLogger();
    
    log.debug("This is a Debug log");
    log.info("This is an Info log");
    log.warn("This is a Warning log");
    log.error("This is an Error log");
  • Custom Configuration:

    const log = logger.createLogger({
      transportOptions: {
        colors: {
          info: "blueBright",
         ...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

How is flexbox different in React Native and browser?

senior

What is Higher Order Component or HOC?

middle

How do you re-render a FlatList?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào