Skip to content

Instantly share code, notes, and snippets.

@malerba118
Created February 17, 2019 00:30
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/8a52d13b62af4dafa5f8fed5d2182cb3 to your computer and use it in GitHub Desktop.
Save malerba118/8a52d13b62af4dafa5f8fed5d2182cb3 to your computer and use it in GitHub Desktop.
Creating a Todo
const useNormalizedApi = () => {
let db = useDB();
return {
...
addTodo: async (text) => {
let todo = await api.addTodo(text);
let { result, entities } = normalize(
todo,
apiSchemas.addTodoResponseSchema
);
db.mergeEntities(entities); // Merge new todo data into database
db.updateStoredQuery('ALL_TODOS', (prevArray) => [...prevArray, todo.id]);
},
...
};
};
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