Created
February 17, 2019 00:27
-
-
Save malerba118/e05440feed68ce4c1ba9d351274a621d to your computer and use it in GitHub Desktop.
Fetching 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
const useNormalizedApi = () => { | |
let db = useDB(); | |
return { | |
... | |
fetchTodos: async () => { | |
let todos = await api.fetchTodos(); | |
let { result, entities } = normalize( | |
todos, | |
apiSchemas.fetchTodosResponseSchema | |
); | |
db.mergeEntities(entities); // Indexed todo data | |
db.updateStoredQuery('ALL_TODOS', result); // List of todo ids | |
}, | |
... | |
}; | |
}; | |
const TodosComponent = (props) => { | |
let db = useDB(); | |
let allTodosQuery = db.getStoredQuery('ALL_TODOS'); | |
let todos = db.executeQuery(allTodosQuery); | |
return ( | |
<JSON data={todos} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment