Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ module.exports = {
browser: true,
es2021: true,
},
extends: [
'plugin:react/recommended',
'airbnb',
],
extends: ['plugin:react/recommended', 'airbnb'],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'react',
],
plugins: ['react'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
Expand Down Expand Up @@ -45,8 +40,9 @@ module.exports = {
'object-curly-spacing': ['error', 'always'],
'key-spacing': ['error', { mode: 'strict' }],
'arrow-spacing': ['error', { before: true, after: true }],

'react/jsx-filename-extension': ['warn', { extensions: ['.js', '.jsx'] }],
'react/prop-types': 'off',
'react/react-in-jsx-scope': 'off',
'import/no-unresolved': 'off',
},
};
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"webpack-dev-server": "^4.8.1"
},
"dependencies": {
"nanoid": "^4.0.2",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
Expand Down
40 changes: 40 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState } from 'react';
import { nanoid } from 'nanoid';
import Header from './components/Header';
import TodoForm from './components/TodoForm';
import TodoLists from './components/TodoLists';

export default function App() {
const [newTodo, setNewTodo] = useState('');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

새롭게 투두로 등록할거라서 newTodo인 것 같아요. 나중에 수정 기능이 생기면 이 값으로 똑같이 사용할 것 같은데 그러면 이 변수의 이름은 newOrUpdateTodo가 되어야만 할거예요.

const [todos, setTodos] = useState([]);

const handleSubmit = (e) => {
e.preventDefault();
setTodos([...todos, { id: nanoid(6), text: newTodo }]);
setNewTodo('');
};

const handleChange = (e) => {
setNewTodo(e.target.value);
};

const handleClickComplete = (clickedItemID) => {
setTodos(todos.filter((item) => item.id !== clickedItemID));
};

return (
<div>
<Header />
<TodoForm
newTodo={newTodo}
onSubmit={handleSubmit}
onChange={handleChange}
/>
<TodoLists
hasTodos={todos.length}
todos={todos}
onClickComplete={handleClickComplete}
/>
</div>
);
}
5 changes: 5 additions & 0 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export default function Header() {
return <div>TO-DO</div>;
}
5 changes: 5 additions & 0 deletions src/components/NoneTodos.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export default function NoneTodos() {
return <span>할 일이 없어요!</span>;
}
16 changes: 16 additions & 0 deletions src/components/ShowTodos.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

export default function ShowTodos({ todos, onClickComplete }) {
return (
<ol>
{todos.map(({ id, text }) => (
<li key={id}>
<span>{text}</span>
<button type="button" id={id} onClick={() => onClickComplete(id)}>
완료
</button>
</li>
))}
</ol>
);
}
10 changes: 10 additions & 0 deletions src/components/TodoForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

export default function TodoForm({ newTodo, onSubmit, onChange }) {
return (
<form onSubmit={onSubmit}>
<input type="text" value={newTodo} onChange={onChange} />
<button type="submit">추가</button>
</form>
);
}
15 changes: 15 additions & 0 deletions src/components/TodoLists.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import NoneTodos from './NoneTodos';
import ShowTodos from './ShowTodos';

export default function TodoLists({ hasTodos, todos, onClickComplete }) {
return (
<>
{hasTodos ? (
<ShowTodos todos={todos} onClickComplete={onClickComplete} />
) : (
<NoneTodos />
)}
</>
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 의도했던 건

<li key={id}>
  <span>{text}</span>
  <button type="button" id={id} onClick={() => onClickComplete(id)}>
    완료
  </button>
</li>

이 부분을 빼는 것을 생각했었어요 ㅎㅎ 여기 코드는 3항 연산자도 좋지만 Guard clause로 작성하는 것도 좋을 것 같아요.

Suggested change
export default function TodoLists({ hasTodos, todos, onClickComplete }) {
return (
<>
{hasTodos ? (
<ShowTodos todos={todos} onClickComplete={onClickComplete} />
) : (
<NoneTodos />
)}
</>
);
}
const isEmpty = (arr = []) => arr.length === 0;
export default function TodoLists({ hasTodos, todos, onClickComplete }) {
if (isEmpty(todos)) {
return <NoneTodos />;
}
return (
<ShowTodos todos={todos} onClickComplete={onClickComplete} />
);
}

See also

Copy link
Author

@ctaaag ctaaag May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 코드 정말 깔끔해졌네요!
저는 hasTodos라는 이름으로 todo의 유무도 컴포넌트 인자로 넘겨주면서 너무 투머치 한 느낌도 있었거든요
isEmpty라는 함수로 빼서 저렇게 사용하는 것도 좋네요!

그리고 처음에 짤 때 가드클래스를 사용했었는데 윤석님이 짜신 것처럼 바로 export default에 넣을 생각을 못했었는데.. 익숙치 않아서 그랬던 것 같습니다!

보내주신 코드에서 지향한 부분 앞으로 코드에도 잘 녹여서 진행해볼게요! 감사해요 :)

5 changes: 5 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('app'));