
간단한 리액트 토이 프로젝트로 투두 리스트를 개발하기로 했다!
github: https://github.com/jwo0o0/Todo-list

우선 피그마로 간단하게 디자인해주고 와이어프레임 만들고 시작했다.
컴포넌트 구조까지 대략적으로 그리고 시작하는게 개발 시간 단축에 효과적인 것 같다.
시작: 2022.09.30 ~
대략적인 컴포넌트 생성 및 배치는 완료한 상태에서
까지 완료
export const TodoList = ({ isDelete, isWrite }) => {
const [todos, setTodos] = useState([]);
const renderTodos = () => {
axios.get("http://localhost:3001/todos?_sort=id&_order=DESC")
.then((res) => {
setTodos(res.data);
})
.catch((error) => {
console.error('ERROR: ', error);
})
}
useEffect(() => {
renderTodos();
}, [])
return (
<TodoListContainer>
<TodoListBox>
<WriteInput isWrite={isWrite} renderTodos={renderTodos}/>
{todos.map((todo) => {
return <Todo
key={todo.id}
id={todo.id}
text={todo.text}
done={todo.done}
isDelete={isDelete}
renderTodos={renderTodos}
/>
})}
</TodoListBox>
</TodoListContainer>
)
}
axios.get("http://localhost:3001/todos?_sort=id&_order=DESC") id 역순으로 정렬 해준다. 이렇게 하면 input창에서 입력하면 바로 아래에 생성된다!
export const WriteInput = ({ isWrite, renderTodos }) => {
const [writeinput, setWriteinput] = useState('');
const writeinputEl = useRef(null);
const handleWriteInputChange = (event) => {
setWriteinput(event.target.value);
}
const handleWriteSubmitBtnClick = () => {
axios({
method: 'post',
url: 'http://localhost:3001/todos',
data: {
text:writeinput,
done: false
}
})
.then(() => {
writeinputEl.current.value = "";
renderTodos();
})
.catch((error) => {
console.error('ERROR: ', error);
})
}
const handleOnKeyPress = (e) => {
if (e.key === 'Enter') {
handleWriteSubmitBtnClick();
}
}
return (
<WriteInputContainer className={isWrite ? "" : "not-active"}>
<label htmlFor="todo_write">Todo</label>
<WriteInputForm
type="text"
id="todo_write"
placeholder="무엇을 할까용?"
maxLength={30}
ref={writeinputEl}
onChange={(e) => {handleWriteInputChange(e)}}
onKeyPress={handleOnKeyPress}
/>
<WriteSubmitBtn onClick={handleWriteSubmitBtnClick}>✓</WriteSubmitBtn>
</WriteInputContainer>
)
}
✏️ enter 키로 생성하기
input 요소에 onKeyPress 이벤트를 등록하고 handleOnKeyPress 이벤트 핸들러에서 키가 Enter인지 확인한 후 생성 작업을 해준다.
✏️ 입력 후 input 창 비우기
useRef로 직접 DOM을 건들여줬다.
input 요소에 ref={input 요소 주소값} 속성을 설정해준다. 그리고 todo 생성 후 writeinputEl(주소값).current.value="" 로 비워주었다!
✏️ 이미지 위에 텍스트 겹치게 배치하기
날짜 표시 부분에서 배경 이미지를 피그마로 만들고 svg 파일로 추출해서 넣어주고, 날짜는 javascript 코드에서 생성되게 했다.
부모 요소 div는 position: relative,
a 요소는 position: absolute top: 40%; left: 50%; transform: translate(-50%, -50%);로 설정
export const TodayDate = styled.div`
width: 194px;
position: relative;
text-align: center;
margin: 0 90px;
> img {
width: 100%;
vertical-align: middle;
}
> a {
position: absolute;
top: 40%;
left: 50%;
text-align: center;
transform: translate(-50%, -50%);
background-color: transparent;
color: #434343;
}
`

const renderTodos = () => {
axios.get("http://localhost:3001/todos?_sort=id&_order=DESC")
.then((res) => {
const notFinishedTodos = res.data.filter(el => el.done === false);
const FinishedTodos = res.data.filter(el => el.done === true);
setTodos([...notFinishedTodos, ...FinishedTodos]);
})
.catch((error) => {
console.error('ERROR: ', error);
})
}
까지 완료했는데 일단 update 기능까지 구현하는걸 목표로 두고 와다다 코딩했더니 추후에 리팩토링을 해야겠다.