Skip to content

Instantly share code, notes, and snippets.

@malerba118
Created February 17, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malerba118/e05440feed68ce4c1ba9d351274a621d to your computer and use it in GitHub Desktop.
Save malerba118/e05440feed68ce4c1ba9d351274a621d to your computer and use it in GitHub Desktop.
Fetching Todos
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