Skip to content

Commit a000756

Browse files
committed
firest commit
1 parent abf75a9 commit a000756

18 files changed

+209
-128
lines changed

Diff for: README.md

+11-58
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,21 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
# React Hooks Example
22

3-
## Available Scripts
3+
1. 함수만으로 React Component 개발
44

5-
In the project directory, you can run:
5+
2. 좀 더 손쉬운 State Management 제공
66

7-
### `yarn start`
7+
3. 커스텀 훅을 만들어 재사용될 소스를 분리
88

9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
9+
4. 별도의 재사용가능한 함수로 사용
1110

12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
11+
5. 복잡한 컴포넌트 계층에서 자식 컴포넌트에서 props로 계속 전달하지 않고 사용할수 있다.
1412

15-
### `yarn test`
13+
6. 코드로 간결하고 깔끔해지며 mobx, redux 없이도 간단한 상태관리가 가능하다.
1614

17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
15+
## start
1916

20-
### `yarn build`
17+
- git clone or Clone or download
2118

22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
19+
- yarn install
2420

25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `yarn eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `yarn build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
21+
- yarn start

Diff for: src/App.css

-22
This file was deleted.

Diff for: src/App.js

-26
This file was deleted.

Diff for: src/App.test.js

-9
This file was deleted.

Diff for: src/components/App.jsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from "react";
2+
import TodoStore from "../context/TodoStore";
3+
import Header from "./Header";
4+
import Form from "./Form";
5+
import List from "./List";
6+
7+
const App = () => {
8+
return (
9+
<TodoStore>
10+
<Header />
11+
<Form />
12+
<List />
13+
</TodoStore>
14+
);
15+
};
16+
17+
export default App;

Diff for: src/components/Form.jsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { useContext, useRef } from "react";
2+
import { TodoContext } from "../context/TodoStore.jsx";
3+
4+
const Form = () => {
5+
const inputRef = useRef(false);
6+
const { dispatch } = useContext(TodoContext);
7+
const addTodoData = e => {
8+
e.preventDefault();
9+
dispatch({ type: "ADD_TODO", payload: inputRef.current.value });
10+
};
11+
return (
12+
<form action="">
13+
<input type="text" ref={inputRef} />
14+
<button onClick={addTodoData}>할일추가</button>
15+
</form>
16+
);
17+
};
18+
19+
export default Form;

Diff for: src/components/Header.jsx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, { useContext } from "react";
2+
import "../css/Header.css";
3+
import { TodoContext } from "../context/TodoStore.jsx";
4+
5+
const Header = () => {
6+
const { todos } = useContext(TodoContext);
7+
8+
return (
9+
<>
10+
<h1>HELLO TODO 애플리케이션</h1>
11+
<div className="countInfo">
12+
해야할일! {todos.filter(v => v.status === "todo").length}개 있습니다.
13+
</div>
14+
</>
15+
);
16+
};
17+
18+
export default Header;

Diff for: src/components/Item.jsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useContext } from "react";
2+
import "../css/Item.css";
3+
import { TodoContext } from "../context/TodoStore.jsx";
4+
5+
const Item = ({ todo }) => {
6+
const { dispatch } = useContext(TodoContext);
7+
8+
const toggleItem = e => {
9+
const id = e.target.dataset.id;
10+
dispatch({ type: "CHANGE_TODO_STATUS", payload: id });
11+
};
12+
13+
const itemClassName = todo.status === "done" ? "done" : "";
14+
15+
return (
16+
<li data-id={todo.id} onClick={toggleItem} className={itemClassName}> {todo.title} </li>
17+
);
18+
};
19+
20+
export default Item;

Diff for: src/components/List.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { useContext } from "react";
2+
import Item from "./Item.jsx";
3+
import { TodoContext } from "../context/TodoStore.jsx";
4+
5+
const List = () => {
6+
const { todos, loading } = useContext(TodoContext);
7+
8+
let todoList = <div>loading...</div>;
9+
if (!loading)
10+
todoList = todos.map(todo => <Item key={todo.id} todo={todo} />);
11+
12+
return <ul>{todoList}</ul>;
13+
};
14+
15+
export default List;

Diff for: src/context/TodoStore.jsx

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { useEffect, useReducer } from "react";
2+
import useFetch from "./useFetch.jsx";
3+
import { todoReducer } from './reducers.jsx';
4+
5+
export const TodoContext = React.createContext();
6+
7+
const TodoStore = (props) => {
8+
const [todos, dispatch] = useReducer(todoReducer, []);
9+
10+
const setInitData = (initData) => {
11+
dispatch({ type: "SET_INIT_DATA", payload: initData })
12+
}
13+
14+
const loading = useFetch(setInitData);
15+
16+
useEffect(() => {
17+
console.log("새로운 내용이 렌더링됐네요", todos);
18+
}, [todos]);
19+
20+
return (
21+
<TodoContext.Provider value={{ todos, loading, dispatch }}>
22+
{ props.children }
23+
</TodoContext.Provider>
24+
);
25+
};
26+
27+
export default TodoStore;

Diff for: src/context/reducers.jsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const todoReducer = (todos, { type, payload }) => {
2+
switch (type) {
3+
case "ADD_TODO":
4+
return [...todos, { id: todos.length, title: payload, status: "todo" }];
5+
6+
case "SET_INIT_DATA":
7+
return payload;
8+
9+
case "CHANGE_TODO_STATUS":
10+
return todos.map(todo => {
11+
if (todo.id === +payload) {
12+
if (todo.status === "done") todo.status = "todo";
13+
else todo.status = "done";
14+
}
15+
return todo;
16+
});
17+
18+
default: break;
19+
}
20+
};

Diff for: src/context/useFetch.jsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useState, useEffect } from "react";
2+
3+
const userData = [
4+
{
5+
id: 1111,
6+
title: "공부하기",
7+
status: "todo"
8+
},
9+
{
10+
id: 2222,
11+
title: "스터디준비",
12+
status: "todo"
13+
},
14+
{
15+
id: 3333,
16+
title: "알고리즘공부",
17+
status: "todo"
18+
},
19+
{
20+
id: 4444,
21+
title: "컴퓨터게임",
22+
status: "todo"
23+
}
24+
];
25+
26+
const useFetch = (callback) => {
27+
const [loading, setLoading] = useState();
28+
29+
const fetchInitialData = () => {
30+
setLoading(true);
31+
callback(userData);
32+
setLoading(false);
33+
};
34+
35+
// 배열 초기값 설정시 한번만 실행 즉, 로딩시 한번만 실행
36+
useEffect(() => {
37+
fetchInitialData();
38+
}, []);
39+
40+
return loading;
41+
};
42+
43+
export default useFetch;

Diff for: src/css/Header.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.countInfo {
2+
margin-bottom: 1rem;
3+
}

Diff for: src/css/Item.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.done {
2+
text-decoration: line-through;
3+
font-style: italic;
4+
}

Diff for: src/index.css renamed to src/css/index.css

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ code {
1111
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
1212
monospace;
1313
}
14+
15+
#root {
16+
padding: 1.5rem;
17+
}

Diff for: src/index.js

-12
This file was deleted.

Diff for: src/index.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import "./css/index.css";
4+
import App from "./components/App";
5+
import * as serviceWorker from "./serviceWorker";
6+
7+
ReactDOM.render(<App />, document.getElementById("root"));
8+
serviceWorker.unregister();

Diff for: src/logo.svg

-1
This file was deleted.

0 commit comments

Comments
 (0)