├── .gitignore
├── src
├── Header.css
├── Item.css
├── App.test.js
├── App.jsx
├── List.jsx
├── Header.jsx
├── index.css
├── index.js
├── useFetch.js
├── Item.jsx
├── App.css
├── reducers.js
├── Form.jsx
├── TodoStore.js
├── logo.svg
└── serviceWorker.js
├── public
├── favicon.ico
├── manifest.json
└── index.html
├── README.md
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
3 |
--------------------------------------------------------------------------------
/src/Header.css:
--------------------------------------------------------------------------------
1 | .countInfo {
2 | margin-bottom: 1rem;
3 | }
--------------------------------------------------------------------------------
/src/Item.css:
--------------------------------------------------------------------------------
1 | .done {
2 | text-decoration: line-through;
3 | font-style: italic;
4 | }
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/crongro/react-hooks-todos-app/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/src/App.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import TodoStore from './TodoStore.js';
3 | import List from './List.jsx';
4 | import Header from './Header.jsx';
5 | import Form from './Form.jsx';
6 | import './App.css';
7 |
8 | const App = () => {
9 | return (
10 |
11 |
12 |
13 |
14 |
15 | )
16 | }
17 |
18 | export default App
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Todo application using React Hooks API(16.8+)
2 |
3 | -----
4 | ## Usage
5 |
6 | ```shell
7 | npm install
8 | npm start
9 | ```
10 | Open [http://localhost:3000]
11 |
12 | -----
13 | ## YouTube lecture
14 | [Youtube lecture](https://www.youtube.com/playlist?list=PLAHa1zfLtLiMukrBDWr-o0q-At7oARwXv)
15 |
16 | ------
17 | \# This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
--------------------------------------------------------------------------------
/src/List.jsx:
--------------------------------------------------------------------------------
1 | import React, {useContext} from 'react'
2 | import Item from './Item.jsx'
3 | import { TodoContext } from './TodoStore.js';
4 |
5 | const List = () => {
6 |
7 | const {todos, loading} = useContext(TodoContext);
8 |
9 | let todoList =
loading...
;
10 | if(!loading) todoList = todos.map( (todo) =>
11 |
12 | )
13 |
14 | return (
15 |
18 | )
19 | }
20 | export default List
--------------------------------------------------------------------------------
/src/Header.jsx:
--------------------------------------------------------------------------------
1 | import React, {useContext} from 'react'
2 | import './Header.css';
3 | import { TodoContext } from './TodoStore.js';
4 |
5 | const Header = () => {
6 | const {todos} = useContext(TodoContext);
7 | return (
8 | <>
9 | HELLO TODO 애플리케이션
10 |
11 | 해야할일 ! {todos.filter(v => v.status === "todo").length}개가 있습니다
12 |
13 | >
14 | )
15 | }
16 |
17 | export default Header
18 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6 | sans-serif;
7 | -webkit-font-smoothing: antialiased;
8 | -moz-osx-font-smoothing: grayscale;
9 | }
10 |
11 | code {
12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13 | monospace;
14 | }
15 |
16 | #root {
17 | padding : 1.5rem;
18 | }
19 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App.jsx';
5 | import * as serviceWorker from './serviceWorker';
6 |
7 | ReactDOM.render(, document.getElementById('root'));
8 |
9 | // If you want your app to work offline and load faster, you can change
10 | // unregister() to register() below. Note this comes with some pitfalls.
11 | // Learn more about service workers: http://bit.ly/CRA-PWA
12 | serviceWorker.unregister();
13 |
--------------------------------------------------------------------------------
/src/useFetch.js:
--------------------------------------------------------------------------------
1 | import { useState, useEffect } from 'react';
2 |
3 | const useFetch = (callback, url) => {
4 |
5 | const [loading, setLoading] = useState(false);
6 |
7 | const fetchInitialData = async () => {
8 | setLoading(true);
9 | const response = await fetch(url);
10 | const initialData = await response.json();
11 | callback(initialData);
12 | setLoading(false);
13 | }
14 |
15 | useEffect ( () => {
16 | fetchInitialData();
17 | }, [])
18 |
19 | return loading;
20 |
21 | }
22 |
23 | export default useFetch;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mytodo",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "react": "^16.8.3",
7 | "react-dom": "^16.8.3",
8 | "react-scripts": "2.1.5"
9 | },
10 | "scripts": {
11 | "start": "react-scripts start",
12 | "build": "react-scripts build",
13 | "test": "react-scripts test",
14 | "eject": "react-scripts eject"
15 | },
16 | "eslintConfig": {
17 | "extends": "react-app"
18 | },
19 | "browserslist": [
20 | ">0.2%",
21 | "not dead",
22 | "not ie <= 11",
23 | "not op_mini all"
24 | ]
25 | }
26 |
--------------------------------------------------------------------------------
/src/Item.jsx:
--------------------------------------------------------------------------------
1 | import React, {useContext} from 'react'
2 | import './Item.css'
3 | import { TodoContext } from './TodoStore.js';
4 |
5 | const Item = ({todo}) => {
6 |
7 | const {dispatch} = useContext(TodoContext);
8 |
9 | const toggleItem = (e) => {
10 | const id = e.target.dataset.id;
11 | dispatch({type:'CHANGE_TODO_STATUS', payload:id});
12 | }
13 |
14 | const itemClassName = todo.status === 'done' ? 'done' : '';
15 |
16 | return (
17 | {todo.title}
18 | )
19 | }
20 |
21 | export default Item
22 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | animation: App-logo-spin infinite 20s linear;
7 | height: 40vmin;
8 | pointer-events: none;
9 | }
10 |
11 | .App-header {
12 | background-color: #282c34;
13 | min-height: 100vh;
14 | display: flex;
15 | flex-direction: column;
16 | align-items: center;
17 | justify-content: center;
18 | font-size: calc(10px + 2vmin);
19 | color: white;
20 | }
21 |
22 | .App-link {
23 | color: #61dafb;
24 | }
25 |
26 | @keyframes App-logo-spin {
27 | from {
28 | transform: rotate(0deg);
29 | }
30 | to {
31 | transform: rotate(360deg);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/reducers.js:
--------------------------------------------------------------------------------
1 | export const todoReducer = (todos, {type, payload}) => {
2 | switch (type) {
3 | case "ADD_TODO":
4 | return [...todos, {'title': payload, 'id' : todos.length, '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:
19 | break;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/Form.jsx:
--------------------------------------------------------------------------------
1 | import React, {useContext, useRef} from 'react'
2 | import { TodoContext } from './TodoStore.js';
3 |
4 | const Form = () => {
5 | const inputRef = useRef(false);
6 | const {dispatch} = useContext(TodoContext);
7 |
8 | const addTodoData = (e) => {
9 | e.preventDefault();
10 | dispatch({type:'ADD_TODO', payload :inputRef.current.value})
11 | }
12 |
13 | return (
14 | <>
15 |
19 | >
20 | )
21 | }
22 |
23 | export default Form
24 |
--------------------------------------------------------------------------------
/src/TodoStore.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useReducer } from 'react';
2 | import useFetch from './useFetch.js';
3 | import {todoReducer} from './reducers.js';
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, 'http://localhost:8080/todo');
15 |
16 | useEffect( ()=> {
17 | console.log("새로운 내용이 렌더링됐네요", todos);
18 | }, [todos])
19 |
20 | return (
21 |
22 | {props.children}
23 |
24 | )
25 | }
26 |
27 | export default TodoStore;
28 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
10 |
11 |
15 |
16 |
25 | React App
26 |
27 |
28 |
29 |
30 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read http://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.1/8 is considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit http://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See http://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl)
104 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------