Skip to content

Commit df9564c

Browse files
committed
1 parent 5c1c9e4 commit df9564c

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
2-
/**/node_modules
2+
/**/node_modules
3+
/.idea

no-redux/src/store.jsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
export const Store =React.createContext();
3+
4+
const initialState ={
5+
episodes : [],
6+
favorites : []
7+
}
8+
9+
function reducer(state, action) {
10+
switch (action.type) {
11+
case 'FETCH_DATA':
12+
return {...state, episodes : action.payload}
13+
case 'ADD_FAVORITE':
14+
return {...state, favorites : [...state.favorites, action.payload]}
15+
case 'REMOVE_FAVORITE':
16+
return {...state, favorites : action.payload}
17+
default:
18+
return state;
19+
}
20+
}
21+
22+
export function StoreProvider(props) {
23+
const [state, dispatch] = React.useReducer(reducer, initialState);
24+
const value = {state, dispatch}
25+
return (<Store.Provider value={value}>{props.children}</Store.Provider>);
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
interface Props {
4+
text : string;
5+
done : boolean;
6+
onToggle() : void;
7+
onRemove() : void;
8+
}
9+
10+
const TodoItem : React.SFC<Props> = ({text, done, onToggle, onRemove}) => (
11+
<li>
12+
<b onClick={onToggle}
13+
style={{
14+
textDecoration : done ? 'line-through' : 'none'
15+
}}>
16+
{text}
17+
</b>
18+
<button style={{all : 'unset', marginLeft : '0.5rem'}} onClick={onRemove}>지우기</button>
19+
</li>
20+
);
21+
22+
export default TodoItem;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import React from 'react';
2+
import TodoItem from './TodoItem'
3+
4+
interface Props {
5+
6+
}
7+
8+
interface TodoItemState {
9+
id : number;
10+
text : string;
11+
done : boolean;
12+
}
13+
14+
interface State {
15+
input : string;
16+
todoItems : TodoItemState[];
17+
}
18+
19+
class TodoList extends React.Component<Props, State> {
20+
nextTodoId : number = 0;
21+
state:State = {
22+
input : '',
23+
todoItems : []
24+
};
25+
26+
onToggle = (id:number): void => {
27+
const { todoItems } = this.state;
28+
const nextTodoItems:TodoItemState[] = todoItems.map(item => {
29+
if(item.id === id) {
30+
item.done = !item.done;
31+
}
32+
return item;
33+
})
34+
35+
this.setState({
36+
todoItems : nextTodoItems
37+
})
38+
}
39+
40+
onSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
41+
e.preventDefault();
42+
const { todoItems, input } = this.state;
43+
const newItem:TodoItemState = { id: this.nextTodoId++, text : input, done : false };
44+
const nextTodoItems:TodoItemState[] = todoItems.concat(newItem);
45+
46+
this.setState({
47+
input : '',
48+
todoItems : nextTodoItems
49+
});
50+
}
51+
52+
onRemove = (id :number): void => {
53+
const { todoItems } = this.state;
54+
const nextTodoItems : TodoItemState[] = todoItems.filter(item => item.id !== id);
55+
this.setState({
56+
todoItems : nextTodoItems
57+
})
58+
}
59+
60+
onChange = (e : React.FormEvent<HTMLInputElement>): void => {
61+
const {value} = e.currentTarget;
62+
this.setState({
63+
input : value
64+
});
65+
}
66+
67+
render() {
68+
const { onSubmit, onChange, onToggle, onRemove } = this;
69+
const { input, todoItems } = this.state;
70+
71+
const todoItemList : React.ReactElement[] = todoItems.map(
72+
todo => (
73+
<TodoItem text={todo.text} done={todo.done} onToggle={()=>onToggle(todo.id)} onRemove={()=>onRemove(todo.id)}/>
74+
)
75+
)
76+
77+
return (
78+
<div>
79+
<h1>오늘 뭐하지?</h1>
80+
<form onSubmit={onSubmit}>
81+
<input onChange={onChange} value={input} />
82+
<button type="submit">추가하기</button>
83+
</form>
84+
<ul>
85+
{todoItemList}
86+
</ul>
87+
</div>
88+
)
89+
}
90+
}
91+
92+
export default TodoList
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface TodoItemDataParams {
2+
id : number;
3+
text : string;
4+
done : boolean;
5+
}

0 commit comments

Comments
 (0)