Hướng dẫn dùng react-native-async-storage JavaScript - sử dụng JavaScript phản ứng gốc-async-storage
Show AsyncStorage là thành phần đơn giản dùng để lưu trữ dữ liệu offline. Nó hỗ trợ lưu trữ dữ liệu không mã hóa theo dạng cặp key-value và hỗ trợ bất đồng bộ Chúng ta có thể lưu dữ liệu độ lớn tùy thuộc vào bộ nhớ của máy do AsyncStorage sẽ tự động sử dụng “Dictionary” hoặc File để lưu giá trị (iOS) hoặc sử dụng RocksDB, Sqlite (Android) Để sử dụng AsyncStorage chúng ta chỉ cần 2 hàm đơn giản sau :
Từ ví dụ trên chúng ta giải thích các câu lệnh như sau : Cách lưu dữ liệu : AsyncStorage.setItem(‘TASKS’, ‘I like to save it.’)TASKS là tên key . ‘I like to save it.’ là value – giá trị muốn lưu Cách lấy dữ liệu : AsyncStorage.getItem(‘TASKS’) .TASKS là tên key muốn lấy giá trị, nếu không có hàm getItem sẽ trả về null Bài tập : Sinh viên tạo 1 màn hình Login với 2 TextInput nhập Username và Password. 1 Button Login và 1 checkbox “Nhớ mật khẩu” sử dụng AsyncStorage để lưu tài khoản đã đăng nhập
2 is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.It is recommended that you use an abstraction on top of 2 instead of 2 directly for anything more than light usage since it operates globally.On iOS, 2 is backed by native code that stores small values in a serialized dictionary and larger values in separate files. On Android, 2 will use either RocksDB or SQLite based on what is available.The 2 JavaScript code is a facade that provides a clear JavaScript API, real 8 objects, and non-multi functions. Each method in the API returns a 9 object.Importing the 2 library:
Persisting data:
Fetching data:
ReferenceMethodsgetItem()
Fetches an item for a 1 and invokes a callback upon completion. Returns a 9 object.Parameters:
setItem()
Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Parameters:
setItem()
Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Parameters:
setItem()
Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Key of the item to set. This is not supported by all native implementations. Parameters:
Example:
setItem() 0Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Parameters:
setItem() 1Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Parameters:
setItem() 2Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object.Key of the item to set. 3value 4Value to set for the 1. Parameters:
Example: 5setItem() 6Sets the value for a 1 and invokes a callback upon completion. Returns a 9 object. 7Value to set for the 1. Parameters:
multiremove () 8Gọi cái này để lô xóa tất cả các khóa trong mảng 0.Trả về một đối tượng 9.Parameters:
Example: 9multiremove () 0Gọi cái này để lô xóa tất cả các khóa trong mảng 0.Trả về một đối tượng 9.Tên: This is not supported by all native implementations. Parameters:
Example: multiremove () |