๋ก์ง ์๊ฐ ํด๋ณด๊ธฐ.
1. ๋๊ธ๋ง๋ค ์ญ์ ํ๋ ๋ฒํผ์ด ์์ผ๋ฉด ๋๊ฒ ๋ค.
2. ๋ฒํผ์ ๋๋ฅด๋ฉด addEventListener() ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด์, ๋๊ธ์ ์ญ์ ํ๋ ๋ก์ง์ด ์คํ๋๋ฉด ๋๊ฒ ๋ค.
์ฝ๋ ๊ตฌํ
1. ๋๊ธ์ด ๋ฌ๋ฆฌ๋ html์ ์๋ ์ญ์ ํ๊ธฐ button ์ถ๊ฐ, class๋ ๋ฌ์์ฃผ์๋ค.
return `<li class="review" data-number="${index}">
<p class="comment-contents">${contents}</p>
<p class="commnet-writer">์์ฑ์: ${writer}</p>
<div>
<button class="update">์์ ํ๊ธฐ</button>
<button class="delete">์ญ์ ํ๊ธฐ</button>
<div>
</li>`
2. ์ญ์ ๋ฒํผ์ ๋ณ์์ ํ ๋นํ๊ธฐ.
const deleteBtns = document.querySelectorAll(".delete"); // ํด๋์ค์ ์ ๊ทผํ ๋๋ ์จ์ , id์ ์ ๊ทผํ ๋๋ #์ ์ฌ์ฉํ๋ค.
console.log(deleteBtns); // ํญ์ ์ฝ์๋ก ํ์ธํด๋ณด๋ ์ต๊ด์ ๋ค์ด์.
์ง์ ํ ์ญ์ ๋ฒํผ์ด ์ฝ์์ ์ ์ฐํ๋์ง ํ์ธ.
ํ์ฌ ๋๊ธ์ ์๋ ๊ฐฏ์๋งํผ ์ ํํ ๊ฐ์ ธ์๋ค.
3. ๊ฐ ๋ฒํผ๋ง๋ค ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ฃผ๊ธฐ ์ํด forEach ๋ก ๋ฐ๋ณต๋ฌธ์ ๋๋ ค ์ด๋ฒคํธ ๋ฐ์์ํค๊ธฐ.
// ๋งค๊ฐ๋ณ์์ index๋ ๋ค์ด๊ฐ ์ ์๋ค๋๊ฑด ์ค๋ ์ฒ์์์๋ค.
// ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด์ฅํ๊ณ ์๋ ๊ธฐ๋ฅ์ด๋ผ๊ณ ํจ. ์ด๊ฑธ ํตํด์ ์ค๋ ๋ณด๋ค ์์ํ๊ฒ(?) ๊ธฐ๋ฅ์ ์คํ์ํจ๊ฒ ๊ฐ๋ค.
deleteBtns.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", function () {
console.log("์ ์๋ํ๋์?")
});
});
๊ทธ๋ฆฌ๊ณ ๊ฐ์ ์ญ์ ํ๊ธฐ ๋ฒํผ์ ๋๋ฌ๋ณธ๋ค.
์ค์ผ์ด ์ด๋ฒคํธ ์๋ ํ์ธํ๊ณ ,
4. toSpliced ๋ฉ์๋ ์ฌ์ฉ
// ์ด๋ค์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํด์ผ ํ ์ง ๊ณ์ ๊ณ ๋ฏผ์ ํ๋ค๊ฐ, data ์ index์ ๊ฐ์ ์ฐ์ด๋ณด์๋ค.
// ์ฌ๊ธฐ์ index๊ฐ ์ฌ์ฉ๋จ !!
deleteBtns.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", function () {
console.log(data);
console.log(index);
});
});
๋ค์ ์ญ์ ํ๊ธฐ ๋ฒํผ์ ๋๋ฌ๋ณด๋ฉด,
์ ์ผ ์ฒซ๋ฒ์งธ ๋ฆฌ๋ทฐ์ ์ญ์ ํ๊ธฐ ๋ฒํผ์ ๋๋ ๋๋ ์ฝ์์ ์ฐ์ data์ ๊ฐ๊ณผ, ์ฒซ๋ฒ์งธ ์ญ์ ๋ฒํผ์ index๊ฐ ๋์๋ค.
์ฌ๊ธฐ์ ์ด๋ค ๋ฉ์๋๋ฅผ ์ฐ๋๊ฒ ์ข์๊น ํ๋ค toSpliced๋ฅผ ์ฌ์ฉํด๋ณด๊ธฐ๋ก ํ๋ค.
์ฌ์ฉ๋ฒ์ mdn ๋ฌธ์๋ฅผ ์ฐธ์กฐํ๋ค.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced
Array.prototype.toSpliced() - JavaScript | MDN
The toSpliced() method of Array instances is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.
developer.mozilla.org
// ์ฌ์ฉ๋ฒ : ๋ฉ์๋ ์์ ๋ฐฐ์ด์ด ์ค๊ณ , ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ํ ์ ๋ฐฐ์ด์ ๋ฐํํจ.
// index ์๋ฆฌ์๋ start index๊ฐ ์ค๊ณ , ์ซ์ "1" ์๋ฆฌ์๋ delete number ์ ๊ฑฐํ ๊ฐ์๋ฅผ ๋ํ๋ด๋ ์ซ์๊ฐ ์จ๋ค.
// ํด์ : index์์ ์์ํด์ "1"๋งํผ delete ํ๋ ๋ฉ์๋
data.toSpliced(index, 1)
deleteBtns.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", function () {
console.log(data.toSpliced(index, 1));
});
});
// ์ด๋ ๊ฒ ์ฝ๋ฉ์ ํ ์ด์ : ๋ด๊ฐ ๋๋ฅด๋ ๋๊ธ์ index ๋ฒํธ๋ฅผ ์ฝ์๋ก ํ์ธํ์ผ๋,
// index ์๋ฆฌ์๋ ํด๋น ๋ฒํผ์ ๊ฐ์ง index๊ฐ ๋ค์ด๊ฐ๋ฉด ๋๊ณ ,
// ์ญ์ ๋ฒํผ์ ํด๋ฆญํ ๋๊ธ๋ง ์ญ์ ํด์ค ๊ฑฐ๋๊น, delete count ๋ 1๋ก ๊ณ ์ ์ํค๋ฉด ๋๋ค.
์ฒซ๋ฒ์งธ ๋ฆฌ๋ทฐ๋ฅผ ์ง์ฐ๊ณ ์ ํ ๋ ์ฝ์๋ก ์ฐ์ด๋ณด๋ฉด,
0๋ฒ์งธ index์ธ 1๋ก ์ด๋ฃจ์ด์ง ๋ฐ์ดํฐ๊ฐ ์ฌ๋ผ์ง๊ณ ๋จ์ data๊ฐ ์ฝ์์ ์ฐํ๋๊ฑธ ํ์ธํ๋ค.
5. ๋ฐ์ดํฐ๋ฅผ localStorage ์ ์ ์ฅ.
deleteBtns.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", function () {
const deletedData = data.toSpliced(index, 1); // toSpliced ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ๊ฐ์ ๋ณ์์ ํ ๋น.
console.log(deletedData); // ์ฝ์๋ก ํ์ธ
const deletedArr = JSON.stringify([...deletedData]); // data๊ฐ์ ๊ฐ์ง ๋ณ์๋ฅผ spread operator๋ฅผ ์ฌ์ฉํด ํ์ด์ฃผ๊ณ JSON.stringify๋ฅผ ์ฌ์ฉํด ๋ฌธ์์ด๋ก ๋ณํ
console.log(deletedArr);
});
});
// data ๊ฐ์ ๊ฐ์ง deletedData๋ฅผ ์๋ก์ด ๋ณ์์ ๋ค์ ํ ๋นํ๋ ์ด์ : ์๋ฌ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ.
์ฝ์๋ก ํ์ธํด๋ณด๋ฉด,
์์ ์ฝ์ = ์คํ๋ผ์ด์ค ๋ฉ์๋ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฅผ ํ ๋นํ ๋ณ์์ ์ฝ์ ์ฐ์ ๊ฐ.
์๋ ์ฝ์ = ์์ data ๊ฐ์ด ํ ๋น๋ ๋ณ์๋ฅผ [] ๋ฐฐ์ด์ spread operator๋ฅผ ์ฌ์ฉํด ํ์ด์ฃผ๊ณ , JSON.stringify๋ฅผ ์ฌ์ฉํด ๋ฌธ์์ด๋ก ๋ณํํด ์ค ๊ฐ.
๋ฌธ์์ด๋ก ๋ณํํ ์ด์ : storage์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ๋ชจ๋ ๋ฌธ์์ด๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์, data๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํด ์ฃผ์๋ค.
๊ฐ์ ๋ฐ๋์ ๋ฌธ์์ด๋ก ์ ์ฅ๋จ
https://velog.io/@bgo517/LocalStorage ์ฐธ์กฐ.
toSpliced ๋ฉ์๋๋ฅผ ํตํด ์์ฑํ ๋ฐฐ์ด์ ์๋ก์ด ๋ณ์์ ํ ๋นํ๋ ์ด์ ๋, ํด๋น ๋ฉ์๋๊ฐ ์ํ์ฐธ์กฐ ์ด์ด์ ๊ทธ๋ ๋ค.
์ํ์ฐธ์กฐ๋ throw error -> toSpliced๊ฐ ์ํ์ฐธ์กฐ์ฌ์ ์๋ฌ๊ฐ ๋ฐ์ -> ์๋ก์ด ๋ณ์์ ์์๋ณต์ฌ
์ด ๋ถ๋ถ์ ๋ช ํํ๊ฒ ์ค๋ช ์ ํ์ง ๋ชปํด์, ์กฐ๊ธ ๋ ์ค๋ฅ๋ฅผ ๋ง๋๋ด์ผ ์ ๊ฒ ๊ฐ๋ค.
์ด์ ๋ ๋ด๊ฐ ์ํ๋ ๊ฐ์ด ๋ฌธ์์ด๋ก ์ ๋ฆฌ๋ data๋ก ์ฝ์์ ์ฐํ๋ ๊ฑธ ํ์ธ ํ์ผ๋, localStorage์ setItem๋ฉ์๋๋ฅผ ์ด์ฉํด ์ ์ฅํด์ฃผ๋ฉด ๋๋ค.
const deleteBtns = document.querySelectorAll(".delete");
console.log(deleteBtns);
deleteBtns.forEach((deleteBtn, index) => {
deleteBtn.addEventListener("click", function () {
const deletedData = data.toSpliced(index, 1);
const deletedArr = JSON.stringify([...deletedData]);
localStorage.setItem(`comment${movieId}`, deletedArr); // `comment${movieId}` ์ด ์๋ฆฌ๋ key๊ฐ์ด ๋ค์ด๊ฐ๊ณ , deletedArr ์ด ์๋ฆฌ๋ ๋ฌธ์์ด๋ก ์ด๋ฃจ์ด์ง value ๊ฐ์ด ๋ค์ด๊ฐ์ผ ํ๋ค.
window.location.reload(); // ์ฐฝ ์๋ก๊ณ ์นจ.
});
});
์ ์ฝ๋๋ ์์ฑ์ฝ๋๋ค. ์ด์ ์์ธํ์ด์ง์ ๊ฐ์ ์ ์์๋ ํ๋์ง ํ์ธ์ ํด๋ณด์ !!
์ ์ผ ์ฒซ๋ฒ์งธ ๋ฆฌ๋ทฐ๋ฅผ ์ญ์ ํด ๋ณธ๋ค.
๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ ์์ ์ผ๋ก ์ญ์ ๊ฐ ๋๊ณ , ์๋ ์๋ก๊ณ ์นจ์ผ๋ก ์ฐฝ์ด ์ต์ ํ ๋๊ฒ์ ๋ณผ ์ ์๋ค.
๊ทธ๋ผ ํ์ฌ localStorage์๋ ๋ด๊ฐ ์ํ๋ data๋ง ๋ค์ด๊ฐ์๋์ง ํ์ธํ๋ค.
1๋ก ์ด๋ฃจ์ด์ง ๋ฐ์ดํฐ๊ฐ ์ฌ๋ผ์ง์ฑ๋ก ๋จ์ ๋ฐ์ดํฐ๊ฐ ์ ์ ์ฅ๋์ด์๋๊ฑธ ํ์ธ ํ ์ ์๋ค.
์ด๋ ๊ฒ ๋๊ธ ์ญ์ ๊ธฐ๋ฅ์ ๊ตฌํํด ๋ณด์๋ค.
์๋ํด ๋ณธ ๊ฒ๋ค
์ค๋ฅ
์์์กฐ์ฐจ ์ด๋ป๊ฒ ํด์ผ๋ ์ง ๋ชฐ๋ผ ๋ฌด์์ localStorage์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ถ์ญ์ ์๋ํด๋ณด๊ธฐ
์๋์ ์ซ์๋ค์ ๋งจ ์ฒ์ ์๊ฐํด๋ณด์๋ ์ญ์ ๊ธฐ๋ฅ ๊ตฌํ ๋ก์ง ์์์ด๋ค.
// 1. ํด๋น์ํ์ ์์ธํ์ด์ง์์๋ local storage์์ ๋ฐ์ดํฐ๋ฅผ ๋ณ์์ ํ ๋นํ๋ค.
const userComment = JSON.parse(localStorage.getItem("comment385687"));
console.log(userComment);
// // 2. ๊ฐ์ ํ ๋นํ ๋ณ์์ toSpliced๋ฉ์๋๋ฅผ ๋ฃ์ด์ค๋ค.
console.log(typeof userComment.toSpliced(0, 1)); // data์ ๊ฐ์ด ์ด๋ค type์ธ์ง ํ์ธํ๋ typeof ๋ฉ์๋์ด๋ค.
console.log(userComment.toSpliced(0, 1));
// 3. toSpliced ๋ฉ์๋๋ฅผ ํตํด ์์ฑํ ๋ฐฐ์ด์ ์๋ก์ด ๋ณ์์ ํ ๋น
const newUserComment = userComment.toSpliced(0, 1);
const arr = JSON.stringify([...newUserComment]);
console.log(newUserComment);
console.log(arr);
// 4. ํด๋น ๋ณ์๋ฅผ ๋ค์ setํด์ค๋ค.
// set ํ๋ ๋ฐฉ๋ฒ ์ค์ ์ฒซ ๋ฒ์งธ ์๊ฐํ ๋ฐฉ๋ฒ / ์ค๋ฅ ๋ฐ์
// 4-1.
console.log(localStorage.setItem("comment385687", arr));
localStorage.setItem("comment385687", arr);
// toSpliced ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๊ฐ์ localStorage ์ set์ ์์ผ๋ณด๋ฉด Uncaught TypeError: JSON.stringfy is not a function
Uncaught TypeError: JSON.stringify is not a function
// ์ค๋ฅ ๋ฐ์,
์์ ์ค๋ฅ์ ์ ์ด์จ ์คํธ๋ง๊ธฐํ์ด๊ฐ ํจ์๊ฐ ์๋๋๋ค๋ผ๋ ์ค๋ฅ๋ฅผ ๋ณด๊ณ ๊ทธ๋ผ ํจ์์ ๋ก์ง์ ํ ๋นํด์ผ ํ๋ ์ถ์ด์
๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ตฌ์ํด ๋ณด์๋ค,
// 4-2. ๋ก์ปฌ์ ์ฅ์์ [{key: comment385687, value: data}] ๊ฐ ์์ผ๋ฉด ๋ถ๋ฌ์ค๊ณ , ๊ทธ๋ ์ง ์๋ค๋ฉด [] ๋น ๋ฐฐ์ด์ ํ ๋น.
const newData = () => {
const dataArray = JSON.parse(localStorage.getItem("comment385687")) || [];
dataArray.push(newUserComment);
localStorage.setItem("comment385687", JSON.stringify(dataArray));
};
newData();
// ์ ์ฝ๋์ ๋ฌธ์ ์ : ๊ธฐ์กด ๋ก์ปฌ์ ์ฅ์์ ์ ์ฅ๋ ๊ธฐ์กด ์ ๋ณด + toSpliced ๋ ๋ฐ์ดํฐ๊ฐ ๋์ ๋จ
// (๋ก์ปฌ์ ์ฅ์ = ์ญ์ ๋ฅผ ์ํ๋ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ค + toSpliced ๋์ด ์ญ์ ํ ๋ฃ์ด์ค ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ)
์ ์ฝ๋๋ ์์ ํ ๋ฐ์ดํฐ๊ฐ localStorage์ ์ ์ฅํ๋๋ฐ ๊น์ง๋ ์ฑ๊ณตํ์ผ๋, ๊ธฐ์กด ๋ฐ์ดํฐ์ ๊ฒ์ผ๋ก ๊ณ์ ์์ฌ๋๊ฐ๋ค.
// 4-3. key๊ฐ ์๋ ํน์ value๋ง ์ญ์ ํ๊ณ ์ถ์ ๋
// ๋ก์ปฌ์ ์ฅ์์ ๋ฐ์ดํฐ๋ฅผ ๋ด๊ณ ์๋ ๋ฐฐ์ด์์
// ์ญ์ ํ๊ณ ์ถ์ ๋ฐ์ดํฐ์ id ๊ฐ์ ๋ฐ์์ ํด๋น ๊ฐ์ด ์๋ ๋ฐ์ดํฐ๋ค๋ง ํํฐ๋งํ์ฌ leftData์ ๋ด๊ณ ๊ทธ leftData๋ฅผ localStorage์ ์๋กญ๊ฒ setItem()์ ํด์ค์ผ ํฉ๋๋ค.
// ๋ก์ปฌ์ ์ฅ์์ ์๋ value๋ฅผ ์ง์ ๊ฐ๊ณตํ๋ ค๊ณ ์๋ํ๋ค๋ฉด, value์ ํ์
์ Object์ด๋ฏ๋ก object์ filterํจ์๋ฅผ ์ ์ฉํ๋ฉด filter is not a function๋ก ๋ํ๋๋ค.
// ์ด๋ฅผ ์ํด ๋ฐ์ดํฐ๋ฅผ ์๋ก์ด ๋ฐฐ์ด์ ๋ด๊ฑฐ๋ Object.values(๊ฐ์ฒด์ด๋ฆ).filter()๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
const deleteData = function (i) {
const [userComment, newData] = useState([]); // const [์ํ๊ฐ, ์ํํจ์] = useState(์ด๊ธฐ๊ฐ)
const newUserComment = userComment.filter((i) => i.writer !== writer);
localStorage.setItem("comment385687", JSON.stringify(newUserComment));
newData(newUserComment);
};
๊ทธ๋ ๊ฒ localStorage์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ ๋ฐฉ๋ฒ์ ๊ตฌ๊ธ๋งํ๋ค๊ฐ useState ๋ผ๋ Hook ์ ๋ํด์ ์๊ฒ ๋์๋ค.
์์ ๋ณ์ ์ ์ธ๋ ๊ตฌ์กฐ๋ถํดํ ๋น์ด ๋ค์ด๊ฐ๋ฉด์, ๋ง์ ์ ๋ณด๋ฅผ ์ ํ๊ฒ ๋๋ ์ ์ตํ ์๊ฐ์ด์์ง๋ง ์์ฝ๊ฒ๋ React์์ ์ฌ์ฉ๋๋ ๋ฌธ๋ฒ์ด๋ผ๊ณ ํ๋ค.
๋์ ๊ณผ์ ๋ ์ค๋ก์ง ์๋ฐ์คํฌ๋ฆฝํธ๋ง ์ฌ์ฉํ ๋ฐ๋๋ผ์๋ฐ์คํฌ๋ฆฝํธ ๊ณผ์ ์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ณ์ ์ฐพ์ ๋ณด์๊ณ , ๋งจ ์์ ์๊ฐํ addEventListener๋ฅผ ์ฌ์ฉํ forEach ๊ตฌ๋ฌธ์ผ๋ก ์ญ์ ๊ธฐ๋ฅ์ ๊ตฌํ์ํฌ ์ ์์๋ค.
๋ด ์์ผ๋ก ์ฒ์ ๊ตฌํํด๋ณธ ์ถ๊ฐ๊ธฐ๋ฅ์ด๋ค.
2~3์ผ์ ๊ฑธ์ณ ๊ตฌํํ ์ถ๊ฐ๊ธฐ๋ฅ์ด๋ค. ์์ง ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ๊น์ง์ ๋ฉ์ธ๋ก์ง์ ๋ ์ฌ๋ฆฌ๋๋ฐ ์์ด์, ์ด๋ ค์์ด ๋ง๋ค.
task ๋ถ์์ ์ข ๋ ์ ๊ฒฝ์จ์ ํด๋ณด์์ผ๊ฒ ๋ค. ์ฌ๊ธฐ์ ๊ธฐ ๊ท๋๋ฅ์ ํตํด ์ ๋ณด๋ฅผ ์ป์์ ์์ด ๋ก์ง ๊ตฌํํ๋๋ฐ ๋ง์ ๋์์ด ๋์๋ค.
ํํํํํํํํ...... ์์ผ๋ก๋ ๋น ํฑ !!
#localStorage #setItem #toSpliced #JSON_stringify_is_not_a_function #spread_operator #addEventListener #delete_๊ธฐ๋ฅ๊ตฌํ #์ญ์ ๊ธฐ๋ฅ์ถ๊ฐ #forEach_index_๋งค๊ฐ๋ณ์_๊ธฐ๋ณธ๋ด์ฅ์์ฑ