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

Tell us some options of storing persisting data in a react native app?

Câu trả lời

In a React Native application, there are several options for storing and persisting data. Each method has its own advantages and is suitable for different use cases. Here are some of the most popular options:

1. AsyncStorage

AsyncStorage is an asynchronous, unencrypted, persistent, key-value storage system provided by React Native. It is suitable for storing small amounts of data, such as user preferences or authentication tokens. However, it is not recommended for large amounts of data or sensitive information due to its lack of encryption.

  • Usage:
    javascript Copy
    import AsyncStorage from '@react-native-async-storage/async-storage';
    
    // Storing data
    const storeData = async (value) => {
      try {
        await AsyncStorage.setItem('my-key', value);
      } catch (e) {
        // saving error
      }
    };
    
    // Retrieving data
    const getData = async () => {
      try {
        const value = await AsyncStorage.getItem('my-key');
        if (value !== null) {
          // value previously stored
        }
      } catch (e) {
        // error reading value
      }
    };
    [10][12][19]

2. SQLite

SQLite is a lightweight, embedded SQL database that is widely used in mobile applications. It is suitable for applications that require complex queries and relational data storage. SQLite can handle larger datasets compared to AsyncStorage.

  • Usage:
    javascript Copy
    import { openDatabase } from 'react-native-sqlite-storage';
    
    const db = openDatabase({ name: 'UserDatabase.db' });
    
    db.transaction(function (txn) {
      txn.executeSql(
        'CREATE TABLE IF NOT EXISTS table_user(user_id INTEGER PRIMARY KEY AUTOINCREMENT, user_name VARCHAR(20), user_contact INT(10), user_address VARCHAR(255))',
        []
      );
    });
    [3][4][6][11]

3. Realm

Realm is an object-oriented database that is designed to run directly inside mobile applications. It is known for its performance and ease of use, especially for applications that require real-time data synchronization and complex data relationships.

  • Usage:
    javascript Copy
    import Realm from 'realm';
    
    const UserSchema = {
      name: 'User',
      properties: {
        name: 'string',
        age: 'int',
      },
    };
    
    const realm = new Realm({ schema: [UserSchema] });
    
    realm.write(() => {
      realm.create('User', { name: 'John', age: 30 });
    });
    [1][7][16][20]

4. React Native Storage

React Native Storage is a wrapper around AsyncStorage that provides additional features such as size control, auto-expiring data, and remote data synchronization.

  • Usage:
    javascript Copy
    import Storage from 'react-native-storage';
    import AsyncStorage from '@react-native-async-storage/async-storage';
    
    const storage = new Storage({
      size: 1000,
      storageBackend: AsyncStorage,
      defaultExpires: 1000 * 3600 * 24,
      enableCache: true,
    });
    
    storage.save({
      key: 'user',
    ...
junior

junior

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

middle

What is flex dimension and how is it different from fixed dimension?

entry

What are hybrid apps?

entry

What are native apps?

Bình luận

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

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