#11useToDo & useLocalStorage Hooks
Overview
Refactor the existing ToDo app by moving all task-related logic into a custom hook useTodo, and persist tasks in localStorage through a separate useLocalStorage hook.
Requirements
- Move the current ToDo logic (state, handlers, etc.) into a custom hook called
useTodo. - Implement a
useLocalStoragehook responsible for synchronizing the ToDo list withlocalStorage. - The ToDo list must:
- Load saved tasks from
localStorageon mount. - Save updated tasks to
localStoragewhenever the list changes.
- Load saved tasks from
- The
useTodohook should expose:- The list of tasks.
- The current input value and a setter.
- Functions to:
- Add a task.
- Delete a task.
- Toggle a task’s completion status.
- Clear completed tasks.
Notes
- The main goal is to separate responsibilities:
useTodohandles app logic.useLocalStoragehandles persistence.
- Keep your hooks pure and reusable — avoid ToDo-specific logic in
useLocalStorage. - When initializing state from
localStorage, remember to handle missing or malformed data safely. - The UI code should not change beyond using
useTodoinstead of local state.
Tests
- adds a task when submitting the form
- toggles task completion and apply line-through when checkbox is clicked
- deletes a task when delete button is clicked
- shows only 'Clear complete' button when there is at least one completed task
- clears completed tasks when 'Clear complete' is clicked
- persists tasks in localStorage across mounts