Skip to content

Commit e773c05

Browse files
committed
Introduce functional updater in useState
1 parent bbca4b3 commit e773c05

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

11/todo-app/src/App.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useReducer, useRef, useCallback } from 'react';
1+
import React, { useRef, useState, useCallback } from 'react';
22
import TodoTemplate from './components/TodoTemplate';
33
import TodoInsert from './components/TodoInsert';
44
import TodoList from './components/TodoList';
@@ -15,47 +15,33 @@ function createBulkTodos() {
1515
return array;
1616
}
1717

18-
function todoReducer(todos, action) {
19-
switch (action.type) {
20-
case 'INSERT': // 새로 추가
21-
// { type: 'INSERT', todo: { id: 1, text: 'todo', checked: false } }
22-
return todos.concat(action.todo);
23-
case 'REMOVE': // 제거
24-
// { type: 'REMOVE', id: 1 }
25-
return todos.filter(todo => todo.id !== action.id);
26-
case 'TOGGLE': // 토글
27-
// { type: 'REMOVE', id: 1 }
28-
return todos.map(todo =>
29-
todo.id === action.id ? { ...todo, checked: !todo.checked } : todo,
30-
);
31-
default:
32-
return todos;
33-
}
34-
}
35-
3618
const App = () => {
37-
const [todos, dispatch] = useReducer(todoReducer, createBulkTodos());
19+
const [todos, setTodos] = useState(createBulkTodos);
3820

3921
// 고유 값으로 사용 될 id
4022
// ref 를 사용하여 변수 담기
41-
const nextId = useRef(2501);
23+
const nextId = useRef(4);
4224

4325
const onInsert = useCallback(text => {
4426
const todo = {
4527
id: nextId.current,
4628
text,
4729
checked: false,
4830
};
49-
dispatch({ type: 'INSERT', todo });
31+
setTodos(todos => todos.concat(todo));
5032
nextId.current += 1; // nextId 1 씩 더하기
5133
}, []);
5234

5335
const onRemove = useCallback(id => {
54-
dispatch({ type: 'REMOVE', id });
36+
setTodos(todos => todos.filter(todo => todo.id !== id));
5537
}, []);
5638

5739
const onToggle = useCallback(id => {
58-
dispatch({ type: 'TOGGLE', id });
40+
setTodos(todos =>
41+
todos.map(todo =>
42+
todo.id === id ? { ...todo, checked: !todo.checked } : todo,
43+
),
44+
);
5945
}, []);
6046

6147
return (

11/todo-app/src/App.reducer.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React, { useReducer, useRef, useCallback } from 'react';
2+
import TodoTemplate from './components/TodoTemplate';
3+
import TodoInsert from './components/TodoInsert';
4+
import TodoList from './components/TodoList';
5+
6+
function createBulkTodos() {
7+
const array = [];
8+
for (let i = 1; i <= 2500; i++) {
9+
array.push({
10+
id: i,
11+
text: `할 일 ${i}`,
12+
checked: false,
13+
});
14+
}
15+
return array;
16+
}
17+
18+
function todoReducer(todos, action) {
19+
switch (action.type) {
20+
case 'INSERT': // 새로 추가
21+
// { type: 'INSERT', todo: { id: 1, text: 'todo', checked: false } }
22+
return todos.concat(action.todo);
23+
case 'REMOVE': // 제거
24+
// { type: 'REMOVE', id: 1 }
25+
return todos.filter(todo => todo.id !== action.id);
26+
case 'TOGGLE': // 토글
27+
// { type: 'REMOVE', id: 1 }
28+
return todos.map(todo =>
29+
todo.id === action.id ? { ...todo, checked: !todo.checked } : todo,
30+
);
31+
default:
32+
return todos;
33+
}
34+
}
35+
36+
const App = () => {
37+
const [todos, dispatch] = useReducer(todoReducer, undefined, createBulkTodos);
38+
39+
// 고유 값으로 사용 될 id
40+
// ref 를 사용하여 변수 담기
41+
const nextId = useRef(2501);
42+
43+
const onInsert = useCallback(text => {
44+
const todo = {
45+
id: nextId.current,
46+
text,
47+
checked: false,
48+
};
49+
dispatch({ type: 'INSERT', todo });
50+
nextId.current += 1; // nextId 1 씩 더하기
51+
}, []);
52+
53+
const onRemove = useCallback(id => {
54+
dispatch({ type: 'REMOVE', id });
55+
}, []);
56+
57+
const onToggle = useCallback(id => {
58+
dispatch({ type: 'TOGGLE', id });
59+
}, []);
60+
61+
return (
62+
<TodoTemplate>
63+
<TodoInsert onInsert={onInsert} />
64+
<TodoList todos={todos} onRemove={onRemove} onToggle={onToggle} />
65+
</TodoTemplate>
66+
);
67+
};
68+
69+
export default App;

11/todo-app/src/components/TodoListItem.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,4 @@ const TodoListItem = ({ todo, onRemove, onToggle, style }) => {
2828
);
2929
};
3030

31-
export default React.memo(
32-
TodoListItem,
33-
(prevProps, nextProps) => prevProps.todo === nextProps.todo,
34-
);
31+
export default React.memo(TodoListItem);

0 commit comments

Comments
 (0)