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.
    javascript Copy
    console.log("This is a log message");
  • console.warn: Logs warnings, typically displayed in a yellow box.
    javascript Copy
    console.warn("This is a warning message");
  • console.error: Logs errors, typically displayed in a red box.
    javascript Copy
    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:
    bash Copy
    npx react-native log-ios
  • For Android:
    bash Copy
    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:

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

    javascript Copy
    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:

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

middle

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

junior

What are the types of data that control a component?

junior

When would you use ScrollView over FlatList or vice-versa?

senior

Are libraries such as TypeScript that compile to JavaScript compatible with React Naive?

Bình luận

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

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