Created
March 1, 2021 17:41
-
-
Save bpesquet/594382b875ef66fa70eaf48aeda544a3 to your computer and use it in GitHub Desktop.
Async storage of todos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AsyncStorage from "@react-native-async-storage/async-storage"; | |
export interface Todo { | |
task: string; | |
completed: boolean; | |
} | |
class TodoService { | |
// Return all todos asynchronously. Returns a Promise | |
async getAll(): Promise<Array<Todo>> { | |
const keys = await AsyncStorage.getAllKeys(); | |
const jsonTodos = await AsyncStorage.multiGet(keys); | |
// For all stored todos, parse value (index = 1) into a object | |
return jsonTodos.map((item) => JSON.parse(item[1])); | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment