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
removeTodo: (id) => { | |
// make a copy first | |
const newTodos = new Map(draftTodos); | |
if (newTodos.delete(id)) { | |
setTodoList(newTodos); | |
} | |
}, | |
addTodo: (message) => { | |
if (!message) { | |
return; |
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
// And inside of the save function, removed existingTodos() as its not a function anymore | |
{ | |
save: () => { | |
const existingTodoKeys = Array.from(existingTodos.keys()); | |
/// ... | |
const existingTodoValues = Array.from(existingTodos.values()); | |
}; | |
} |
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]); |
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
<TodoContext.Provider | |
value={{ | |
save: () => { | |
// contrived code for the demonstration | |
// in the real app this was responsible for deciding if a request should be sent to server or not | |
const existingTodoKeys = Array.from(existingTodos().keys()); | |
const draftTodoKeys = Array.from(draftTodos.keys()); | |
let todosHasChanges = existingTodoKeys.length !== draftTodoKeys.length; | |
// now check entries using ids, unless we know they have changed based on length | |
// there are better ways of detecting changes but this demonstrates the issue |
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
<TodoContext.Provider | |
value={{ | |
todoItems: draftTodos ? Array.from(draftTodos.values()) : [], | |
removeTodo: (id) => { | |
if (draftTodos.delete(id)) { | |
setTodoList(new Map(draftTodos)); | |
} | |
}, | |
addTodo: (message) => { | |
if (!message) { |
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
export const TodoProvider = ({ children }) => { | |
// New line here! | |
const [draftTodos, setTodoList] = useState(); | |
const [apiResponse, setApiResponse] = useState(undefined); | |
useEffect(() => { | |
const fetchTodos = async () => { | |
const res = await fetch("./todos.json"); | |
const response = await res.json(); | |
setApiResponse(response); |
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
{ | |
1: { | |
"id": 1, | |
"message": "Go to the supermarket", | |
"done": false | |
}, | |
2: { | |
"id": 2, | |
"message": "Mow the lawn", | |
"done": true |
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
export const TodoContext = createContext(); | |
export const TodoProvider = ({ children }) => { | |
const [apiResponse, setApiResponse] = useState(undefined); | |
const [draftTodos, setTodoList] = useState(); | |
useEffect(() => { | |
const fetchTodos = async () => { | |
const res = await fetch("./todos.json"); | |
const response = await res.json(); |
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
{ | |
"todos": [ | |
{ | |
"id": 1, | |
"message": "Go to the supermarket", | |
"done": false | |
}, | |
{ | |
"id": 2, | |
"message": "Mow the lawn", |
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
import { useTodos } from "./TodoContext"; | |
export const AddTodo = () => { | |
const { addTodo } = useTodos(); | |
return ( | |
<form | |
onSubmit={(e) => { | |
e.preventDefault(); | |
const formEntries = new FormData(e.target); |
NewerOlder