Skip to content

Instantly share code, notes, and snippets.

@Georgegriff
Created January 10, 2022 10:43
Show Gist options
  • Save Georgegriff/0e717f7168b5eb7ff50d475a0a017475 to your computer and use it in GitHub Desktop.
Save Georgegriff/0e717f7168b5eb7ff50d475a0a017475 to your computer and use it in GitHub Desktop.
hooks mutations - switch to useMemo
// main change - change to use useMemo which means existingTodos is the todos, not a function.
const existingTodos = useMemo(() => {
const todoMap = new Map();
apiResponse?.todos.forEach((todo) => {
todoMap.set(todo.id, todo);
});
return todoMap;
}, [apiResponse]);
useEffect(() => {
// removed `existingTodos` from being a function
if (!draftTodos && existingTodos.size) {
setTodoList(existingTodos);
}
}, [existingTodos]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment