Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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',
},
};
36 changes: 36 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState } from 'react';
import Header from './components/Header';
import TodoInput from './components/TodoInput';
import TodoLists from './components/TodoLists';

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

Choose a reason for hiding this comment

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

input이라는 이름이 붙은 이유가 <input />태그로 쓰이거나 혹은 사용자의 입력으로 사용되는 텍스트라서 그런 것 같네요. 만약 이러한 텍스트가 LocalStorage나 외부 API를 통해서 값이 변경될 수 있다면 그때는 이름을 변경해야겠네요

const [todos, setTodos] = useState([]);
const randomId = () => Math.random().toString(12).substr(2, 16);
Copy link
Contributor

Choose a reason for hiding this comment

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

아이디를 만들기 위해서 랜덤한 값으로 만들고 계시군요. 만약에 아이디가 겹치지 않도록 하려면 UUID를 사용하거나 nanoid같은 것을 사용하는 것이 좋겠어요. 혹은 값을 1부터 계속 증가시켜서 겹치지 않도록 할 수 있습니다.


const handleSubmit = (e) => {
e.preventDefault();
setTodos([...todos, { id: randomId(), text: inputText }]);
setInputText('');
};

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

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

return (
<div>
<Header />
<TodoInput
Copy link
Contributor

Choose a reason for hiding this comment

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

이 컴포넌트 이름이 <TodoForm>이 되는 것은 어떨까요?

inputText={inputText}
onSubmit={handleSubmit}
onChange={handleChange}
/>
<TodoLists 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>;
}
10 changes: 10 additions & 0 deletions src/components/TodoInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

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

export default function TodoLists({ todos, onClickComplete }) {
const showTodos = () => {
if (todos.length === 0) {
return <span>할 일이 없어요!</span>;
}
return todos.map((item) => (
<li key={item.id}>
<span>{item.text}</span>
<button type="button" id={item.id} onClick={onClickComplete}>
완료
</button>
</li>
));
};
Copy link
Contributor

Choose a reason for hiding this comment

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

이부분이 복잡하다고 생각하셔서 함수를 만드신 것 같습니다. 컴포넌트로 분리하는게 더 좋아보이네요.


return <ol>{showTodos()}</ol>;
}
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'));