Created
January 10, 2022 10:43
-
-
Save Georgegriff/0e717f7168b5eb7ff50d475a0a017475 to your computer and use it in GitHub Desktop.
hooks mutations - switch to useMemo
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
// 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