-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React diary with crud dev #1
base: pr-jihee
Are you sure you want to change the base?
Changes from all commits
bfc174d
8fc9257
01070c0
999a850
cba6c07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import "./App.css"; | ||
import { useState } from "react"; | ||
import React, { useState } from "react"; | ||
import Form from "./components/Form"; | ||
import List from "./components/List"; | ||
|
||
|
@@ -13,51 +13,55 @@ const initialDiaryData = localStorage.getItem("diaryData") | |
|
||
function App() { | ||
const [diaryData, setDiaryData] = useState(initialDiaryData); | ||
const [dateData, setDateData] = useState(initialDateData); | ||
const [diaryText, setDiaryText] = useState(""); | ||
const [diaryDate, setDiaryDate] = useState(""); | ||
|
||
|
||
// 일기 추가가 안 되는데 문제가 뭔지 모르겠음. id 문제인가? | ||
// * Create 기능 | ||
const handleSubmitDiary = (e) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e 는 뭔가요?! 어떤 역할을 하나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. event가 일어났을 때 수행되는 함수라서 해당 event를 매개변수로 받아오는 겁니다! |
||
|
||
e.preventDefault(); | ||
console.log(e.dateValue) | ||
console.log(e.diaryValue) | ||
|
||
|
||
// 새로운 일기 데이터, 날짜 데이터 | ||
let newDiary = { | ||
id: Date.now(), | ||
date: | ||
diaryValue: diaryText, | ||
diaryDate: diaryDate | ||
}; | ||
|
||
// 기존 일기에 새로운 일기, 날짜 추가 | ||
setDiaryData((prev) => [...prev, newDiary]); | ||
setDiaryData((prev) => [newDiary, ...prev]); | ||
Jsoo-Kim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
localStorage.setItem("diaryData", JSON.stringify([newDiary, ...diaryData])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// 입력란에 있던 글씨 지워주기 | ||
setDiaryText(""); | ||
setDiaryDate(""); | ||
}; | ||
|
||
// 전체 삭제 | ||
const handleRemoveAll = () => { | ||
setDiaryData([]); | ||
localStorage.setItem("diaryData", JSON.stringify([])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모든 데이터를 지우는 메서드로 이해했습니다. setDiaryData([]);
localStorage.setItem("diaryData", JSON.stringify([])); 와의 차이점이 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 그런 방법이 있었네요! 한 줄 일기 앱의 경우에는 localStorage에 저장된 데이터가 어차피 한 종류이기 때문에 그 메서드를 사용해도 될 것 같습니다! |
||
}; | ||
|
||
// * html 부분 | ||
return ( | ||
<div> | ||
<div> | ||
<h1>한 줄 일기</h1> | ||
<button onClick={handleRemoveAll}>Delete All</button> | ||
</div> | ||
<div> | ||
<Form | ||
handleSubmitDiary={handleSubmitDiary} | ||
diaryText={diaryText} | ||
setDiaryText={setDiaryText} | ||
diaryDate={diaryDate} | ||
setDiaryDate={setDiaryDate} | ||
/> | ||
<List | ||
// diaryValue={diaryValue} | ||
// dateValue={dateValue} | ||
diaryText={diaryText} | ||
diaryData={diaryData} | ||
setDiaryData={setDiaryData} | ||
dateData={dateData} | ||
setDateData={setDateData} | ||
/> | ||
</div> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,25 @@ | ||
import React, { useState } from "react"; | ||
|
||
const List = ({ | ||
id, | ||
diaryValue, | ||
dateValue, | ||
diaryData, | ||
setDiaryData, | ||
dateData, | ||
setDateData, | ||
}) => { | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [editedDateText, setEditedDateText] = useState(dateValue); | ||
const [editedDiaryText, setEditedDiaryText] = useState(diaryValue); | ||
|
||
// 업데이트 할 때 입력 받는 부분? | ||
const handleDateEditChange = (e) => { | ||
setEditedDateText(e.target.dateText); | ||
}; | ||
|
||
const handleDiaryEditChange = (e) => { | ||
setEditedDiaryText(e.target.diaryText); | ||
}; | ||
|
||
// * Update 기능 | ||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
let newDiaryData = diaryData.map((data) => { | ||
if (data.id === id) { | ||
data.title = editedDiaryText; | ||
} | ||
return data; | ||
}); | ||
setDiaryData(newDiaryData); | ||
localStorage.setItem("diaryData", JSON.stringify(newDiaryData)); | ||
|
||
let newDateData = dateData.map((data) => { | ||
if (data.id === id) { | ||
data.title = editedDateText; | ||
} | ||
return data; | ||
}); | ||
setDateData(newDateData); | ||
localStorage.setItem("dateData", JSON.stringify(newDateData)); | ||
|
||
setIsEditing(false); | ||
}; | ||
|
||
// * Delete 기능 | ||
const handleRemove = (id) => { | ||
let newDiaryData = diaryData.filter((data) => data.id !== id); | ||
setDiaryData(newDiaryData); | ||
localStorage.setItem("diaryData", JSON.stringify(newDiaryData)); | ||
|
||
let newDateData = dateData.filter((data) => data.id !== id); | ||
setDateData(newDateData); | ||
localStorage.setItem("dateData", JSON.stringify(newDateData)); | ||
}; | ||
|
||
// * html 부분 | ||
if (isEditing) { | ||
/* isEditing이 true 즉, 수정 중인 데이터 */ | ||
return ( | ||
<div> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
className="editDateText" | ||
value={editedDateText} | ||
onChange={handleDateEditChange} | ||
/> | ||
<input | ||
className="editDiaryText" | ||
value={editedDiaryText} | ||
onChange={handleDiaryEditChange} | ||
autoFocus /* 웹 페이지를 열었을 때 해당 요소에 자동으로 커서가 위치하여 사용자가 바로 입력을 시작할 수 있게 함 */ | ||
/> | ||
</form> | ||
|
||
<div className="isEditingButtons"> | ||
<button onClick={() => setIsEditing(false)} type="button"> | ||
X | ||
</button> | ||
<button onClick={handleSubmit} type="submit"> | ||
Save | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} else { | ||
/* isEditing이 false 즉, 작성이 완료된 데이터 */ | ||
return ( | ||
<div> | ||
<span className="editedDate"> | ||
{dateValue} | ||
</span> | ||
<span className="editedDiary"> | ||
{diaryValue} | ||
</span> | ||
|
||
<div className="isEditedButtons"> | ||
<button onClick={() => handleRemove(id)} className="removeButton"> | ||
X | ||
</button> | ||
<button onClick={() => setIsEditing(true)} className="editButton"> | ||
Edit | ||
</button> | ||
import React from "react"; | ||
import ListItem from "./ListItem"; | ||
|
||
const List = ({ diaryData, setDiaryData }) => { | ||
return ( | ||
<div className="listItems"> | ||
<div style={{ display: "flex" }}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인라인 방식으로 css 를 설정하신 이유가 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. css를 안 하고 만들다가 너무 불편해서 배치된 아이템 일부만 보기 편하게 정렬했습니다..! |
||
<div> | ||
{diaryData.map((data) => ( | ||
<ListItem | ||
key={data.key} | ||
id={data.id} | ||
diaryValue={data.diaryValue} | ||
diaryDate={data.diaryDate} | ||
diaryData={diaryData} | ||
setDiaryData={setDiaryData} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
export default List; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useState } from "react"; | ||
|
||
const ListItem = ({ id, diaryValue, diaryDate, diaryData, setDiaryData }) => { | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [editedDiaryValue, setEditedDiaryValue] = useState(diaryValue); // Edit 할 때 입력되어 있는 부분 | ||
const [editedDiaryDate, setEditedDiaryDate] = useState(diaryDate); | ||
|
||
|
||
// 업데이트 할 때 입력 받는 부분? | ||
const handleDiaryEditChange = (e) => { | ||
const { name, value } = e.target; | ||
if (name === "diaryValue") { | ||
setEditedDiaryValue(e.target.value); | ||
} else if (name === "diaryDate") { | ||
setEditedDiaryDate(e.target.value); | ||
} | ||
}; | ||
|
||
// * Update 기능 (save 클릭했을 때 호출) | ||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
let newDiaryData = diaryData.map((data) => { | ||
if (data.id === id) { | ||
data.diaryValue = editedDiaryValue; | ||
data.diaryDate = editedDiaryDate; | ||
} | ||
return data; | ||
}); | ||
setDiaryData(newDiaryData); | ||
localStorage.setItem("diaryData", JSON.stringify(newDiaryData)); | ||
|
||
setIsEditing(false); | ||
}; | ||
|
||
// * Delete 기능 | ||
const handleRemove = (id) => { | ||
let newDiaryData = diaryData.filter((data) => data.id !== id); | ||
setDiaryData(newDiaryData); | ||
localStorage.setItem("diaryData", JSON.stringify(newDiaryData)); | ||
}; | ||
|
||
// * html 부분 | ||
if (isEditing) { | ||
/* isEditing이 true 즉, 수정 중인 데이터 */ | ||
return ( | ||
<div className="stateOfEditing"> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
className="editDiaryDate" | ||
name="diaryDate" | ||
value={editedDiaryDate} | ||
onChange={handleDiaryEditChange} | ||
/> | ||
<input | ||
className="editDiaryValue" | ||
name="diaryValue" | ||
value={editedDiaryValue} /* 저장된 텍스트 값 부분 */ | ||
onChange={handleDiaryEditChange} | ||
autoFocus /* 웹 페이지를 열었을 때 해당 요소에 자동으로 커서가 위치하여 사용자가 바로 입력을 시작할 수 있게 함 */ | ||
/> | ||
</form> | ||
|
||
<div className="isEditingButtons"> | ||
<button onClick={() => setIsEditing(false)} type="button"> | ||
X | ||
</button> | ||
<button onClick={handleSubmit} type="submit"> | ||
Save | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} else { | ||
/* isEditing이 false 즉, 작성이 완료된 데이터 */ | ||
return ( | ||
<div style={{ display: "flex" }} key={id}> | ||
{" "} | ||
{/* 각 리스트들의 고유 key 가 필요함 */} | ||
<span className="editDate"> | ||
{diaryDate} | ||
</span> | ||
<span className="editDiary"> | ||
{diaryValue} | ||
</span> | ||
<div className="isEditedButtons"> | ||
<button onClick={() => handleRemove(id)} className="removeButton"> | ||
X | ||
</button> | ||
<button onClick={() => setIsEditing(true)} className="editButton"> | ||
Edit | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export default ListItem; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import
문에React
가 추가되었네요!React
는 어떤 역할을 하나요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제 기억으로는 React 부분이 없을 때 오류가 나는지 시험해 봤던 것 같습니다!
결과는 -> useState는 React 라이브러리의 일부분이기 때문에, React를 가져오지 않고 useState만 사용하면 오류가 발생합니다.
아니면 React 컴포넌트를 사용하기 위해 import React from 'react';를 추가로 선언해야 한다고 합니다. 이 부분은 다시 시험해 봐야 할 것 같습니다!