UMRE: Section 33, React Hooks Project
Section 33: React Hooks Project
252. Intro to Hooks Project
253. Adding Our Form With Hooks
254. Adding Todo Item Component
- GRID from material UI – center with justify=”center”
- GRID from material UI is just like bootstrap
ex:
<Grid container justify="center" style=>
<Grid item xs={11} md={8} lg={4}>
<TodoForm addTodo={addTodo} />
<TodoList todos={todos} />
</Grid>
</Grid>
**** ADDED @material-ui/icons
255. Toggling and Deletion w/ Hooks
-
Stateful parent and we pass it down!
- Add RemoveTodo
- Add ToggleTodo
- Add DeleteTodo
**** ADDED uuid
How to toggle with map:
const toggleTodo = (todoId) => {
const updatedTodos = todos.map((todo) =>
todo.id === todoId ? { ...todo, completed: !todo.completed } : todo
);
setTodos(updatedTodos);
};
256. Editing w/ Hooks
-
Toggle Edit ON and OFF useToggle State add hook to Todo.js isEditing, toggle, useToggleState(false)
-
Make the form Then make the form EditTodoForm
-
Populate the form useInputState()
-
Make the method that will update the that todo when we actually submit
257. Small Style Tweaks
- Add divider back
- No divider at bottom
- Margin on left
- Width of text field
- Add Autofocus
258. LocalStorage w/ UseEffect Hook
- useEffect()
- Can send it to local storage
window.localStorage.setItem('todos', JSON.stringify(todos))
- for local storageJSON.stringify()
- localStorage.getItems –
JSON.parse(window.localStorage.getItem('todos'))
- useEffeect runs every rerender, so we should add [todos] as a secondary argument to useEffect
- Add if else to TodosList
259. Refactoring to a Custom Hook
- Use todo state
- Move it all over
- add state
- add initial value (initialTodosd)
- Add destructing to Todo.js
260. Creating our UseLocaslStorateState Hook
- Make a new custom hook to use localstorage – useLocalStorage()
- Make new file with function and export like normal
- define function that takes a key and a default value
- FIRST??make a piece of state based off of value in localstorage
WE CAN PASS A FUNCTION INTO USE STATE
we can pass a try/catch into useState
- let val
- try (to get from local storage)
- catch(e) error val = defaultVal
- MUST RETURN SOMETHING
- SECOND: use useEffect to update localStorage when state Changes
- useLocalStorageState in useTodoState
- getAngry button is goal
NOTES: this is