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

What is AsyncStorage ...

Câu trả lời

AsyncStorage is an asynchronous, unencrypted, persistent, key-value storage system for React Native applications. It allows developers to store data locally on the device, which can be useful for persisting user preferences, session data, or any other small pieces of information that need to be retained across app sessions.

Key Features of AsyncStorage:

  1. Asynchronous Operations: AsyncStorage operations are non-blocking, meaning they do not interfere with the main thread, which helps in maintaining the app's performance.
  2. Persistent Storage: Data stored in AsyncStorage persists across app restarts and device reboots.
  3. Key-Value Storage: It uses a simple key-value pair system to store data, similar to localStorage in web development.

How to Use AsyncStorage:

Installation:

To use AsyncStorage, you need to install the @react-native-async-storage/async-storage package. This can be done using npm or yarn:

bash Copy
npm install @react-native-async-storage/async-storage
# or
yarn add @react-native-async-storage/async-storage

Importing:

After installation, you can import AsyncStorage into your React Native components:

javascript Copy
import AsyncStorage from '@react-native-async-storage/async-storage';

Storing Data:

To store data, you use the setItem method. This method takes a key and a value, both of which must be strings. If you need to store objects, you should serialize them using JSON.stringify().

javascript Copy
const storeData = async (key, value) => {
  try {
    await AsyncStorage.setItem(key, value);
  } catch (e) {
    // saving error
    console.error(e);
  }
};

// Example of storing an object
const storeObject = async (key, object) => {
  try {
    const jsonValue = JSON.stringify(object);
    await AsyncStorage.setItem(key, jsonValue);
  } catch (e) {
    // saving error
    console.error(e);
  }
};

Retrieving Data:

To retrieve data, you use the getItem method. This method returns a promise that resolves to the stored value. If the value is an object, you need to parse it using JSON.parse().

javascript Copy
const getData = async (key) => {
  try {
    const value = await AsyncStorage.getItem(key);
    if (value !== null) {
      // value previously stored
      return value;
    }
  } catch (e) {
    // error reading value
    console.error(e);
  }
};

// Example of retrieving an object
const getObject = async (key) => {
  try {
    const jsonValue = await AsyncStorage.getItem(key);
    return jsonValue != null ? JSON.parse(jsonValue) : null;
  } catch (e) {
    // error reading value
    console.error(e);
  }
};

Removing Data:

To remove a specific item, you use the removeItem method:

javascript Copy
const removeData = async (key) => {
  try {
    await AsyncStorage.removeItem(key);
  } catch (e) {
    // remove error
    console.error(e);
  }
};

Clearing All Data:

To clear all data stored in AsyncStorage, you use the clear method:

javascript Copy
const clearAl...
senior

senior

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

middle

How do you re-render a FlatList?

senior

State the lifecycle of Gesture Responder System?

expert

Does React Native have a Virtual DOM?

Bình luận

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

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