๐ REACT ๋ฝ๋ชจ๋๋ก ํ์ด๋จธ ๋ง๋ค๊ธฐ (7) ๐
๋ง์ง๋ง์ผ๋ก Study ํ์ด์ง๋ฅผ ๋ง๋ค์๋ค.
Study ํ์ด์ง๋ Study ์ปดํฌ๋ํธ์ ๋ฐ๋์ ํ์ ์ ๋ด๋นํ๋ ChimStudy ์ปดํฌ๋ํธ๋ก ๊ตฌํํ์๋ค.
ChimStudy ์ปดํฌ๋ํธ
// ChimStudy.js
import { useEffect, useState } from "react";
import Chim from "../chim/Chim";
import "../BackGround/Background.css";
export default function ChimStudy({ isStart, setIsStart, degree }) {
const savedDegree = parseInt(localStorage.getItem("degree") || degree);
const [currentDegree, setCurrentDegree] = useState(savedDegree);
useEffect(() => {
setCurrentDegree(degree);
}, [degree]);
useEffect(() => {
if (!isStart) {
return;
}
let interval = setInterval(() => {
setCurrentDegree((prev) => {
if (prev <= 0) {
clearInterval(interval);
setIsStart(false);
localStorage.setItem("isStart", "false");
return 0;
}
const newDegree = prev - 1;
localStorage.setItem("degree", newDegree.toString());
return newDegree;
});
}, 1000);
return () => {
clearInterval(interval);
};
}, [isStart, degree]);
return (
<Chim degree={currentDegree}/>
)
}
- props๋ก ํ์ด๋จธ ์คํ ์ฌ๋ถ์ธ isStart, ํ์ด๋จธ ๋๋ฌ์ ๋ false๋ก ๋ฒ๊ฒฝํ๊ธฐ ์ํ setIsStart ํจ์, ์ด๊ธฐ ํ์ ๊ฐ๋์ธ degree ์ธ ๊ฐ์ง ๊ฐ์ ๋ฐ๋๋ค.
- localStorage.getItem์ ํตํด ์ ์ฅ๋ degree ๊ฐ์ ๋ฐ์์ ์ ์๋ก ๋ณํํ์ฌ savedDegree์ ์ ์ฅํ๋ค. ๊ฐ์ ๋ถ๋ฌ์ฌ ์ ์๋ ๊ฒฝ์ฐ ๋ถ๋ชจ์๊ฒ์ ์ ๋ฌ๋ degree ๊ฐ์ ์ฌ์ฉํ๋ค.
- currentDegree์ ํ์ฌ ๋ฐ๋์ ํ์ ๊ฐ๋๋ฅผ ์ ์ฅํ๋ค. useState๋ฅผ ์ฌ์ฉํ๊ธฐ์ ํ์ด์ง๊ฐ ์๋ก๊ณ ์นจ๋์ด๋ ๊ฐ์ ์ ์งํ ์ ์๋ค.
- useEffect ๋ด์์ isStart๊ฐ ๊ฑฐ์ง์ด๋ฉด setInterval์ด ์คํ๋์ง ์๋๋ค.
- cureentDegree ๊ฐ <= 0 ์ผ ๊ฒฝ์ฐ clearInterval(interval); setIsStart(false);์ ํตํด ํ์ด๋จธ๊ฐ ๋ฉ์ถ๋ค.
- newDegree๋ ๊ฐ์๋ currentDegree ๊ฐ์ ์ ์ฅํ๋ ๋ณ์์ด๋ฉฐ ์ด๋ฅผ ํตํด ๋ฐ๋์ด ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก ํ์ ํ๋ค. localStorage.setItem(ํค, ๊ฐ)์ ํตํด localStorage์๋ "degree": "๊ฐ"์ ํํ๋ก ์ ์ฅํ๋ค.
- ์ปดํฌ๋ํธ๊ฐ ์ธ๋ง์ดํธ๋ ๋ return () => {clearInterval(interval);}์ด ์คํ๋์ด setInterval์ ์ ๋ฆฌํ๋ค.
- currentDegree ๊ฐ์ Chim ์ปดํฌ๋ํธ์ ์ ๋ฌํ์ฌ currentDegree ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง๋ค ๋ฐ๋์ด ํ์ ํ๊ฒ ๋๋ค.
Study ์ปดํฌ๋ํธ
// Study.js
import { useState, useEffect } from "react";
import Background from "../BackGround/Background";
import TomatoBtn from "../TomatoBtn/TomatoBtn";
import ChimStudy from "./ChimStudy";
import pic4 from "../Pic/TomatoClock.png";
import "./Study.css";
export default function Study() {
const savedIsStart = localStorage.getItem("isStart") === "false";
const savedDegree = localStorage.getItem("degree") || "0";
const [isStart, setIsStart] = useState(savedIsStart);
const [currentDegree, setCurrentDegree] = useState(savedDegree);
const studyM = parseInt(localStorage.getItem("studyM") || "0");
const breakM = parseInt(localStorage.getItem("breakM") || "0");
const handleStart = () => {
setIsStart(true);
setCurrentDegree(studyM * 6);
localStorage.setItem("isStart", "true");
localStorage.setItem("degree", studyM * 6);
};
const handleBreak = () => {
setIsStart(true);
setCurrentDegree(breakM * 6);
localStorage.setItem("isStart", "true");
localStorage.setItem("degree", breakM * 6);
};
useEffect(() => {
return () => {
localStorage.setItem("isStart", isStart.toString());
localStorage.setItem("degree", currentDegree.toString());
};
}, [isStart, currentDegree]);
return (
<>
<Background title='STUDY' />
<div className="main">
<div className="btn">
<TomatoBtn name='START' className="to" onClick={handleStart} />
<TomatoBtn name='BREAK' className="to" onClick={handleBreak} />
</div>
<div className="CheckTomato">
<img src={pic4} alt='pic4' className="pic4" />
<ChimStudy isStart={isStart} setIsStart={setIsStart} degree={currentDegree} />
</div>
</div>
</>
)
}
- localStorage์์ isStart ๊ฐ์ ๊ฐ์ ธ์ false๋ฉด true๋ก true๋ฉด false๋ก ์ค์ ํ์ฌ savedIsStart์ ์ ์ฅํ๋ค.
- handleStart์ handleBreak๋ฅผ ํตํด setIsStart ์ํ๋ฅผ true๋ก ๋ณ๊ฒฝํ๊ณ currentDegree์ ๊ณต๋ถ(ํด์) ์๊ฐ์ degree๋ก ๋ณํํ์ฌ ์ ์ฅํ๋ค.
- isStart ๋๋ currentDegree ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋ localStorae์ isStart, degree๋ฅผ ์ ์ฅํ๋ค.
// Study.css
.main {
display: flex;
margin: 0 200px 0 200px;
}
.btn {
margin: 80px 80px 0 0;
}
.pic4 {
width: 687.8px;
height: 519px;
}
.CheckTomato {
position: relative;
}
์ด๋ ๊ฒ ๋ค ํ์ด์ง๋ก ์ด๋ฃจ์ด์ง ๋ฝ๋ชจ๋๋ก ํ์ด๋จธ๋ฅผ ์์ฑํ์๋ค.
์ฝ๋๊ฐ ์ ๋์๊ฐ๋์ง ํ์ธํด์ฃผ์ด์ผ ํ๊ธฐ์ console.log๋ฅผ ์๊ฐ๋ณด๋ค ์์ฃผ, ๊ผผ๊ผผํ ์ฌ์ฉํด์ผ ํ๋ค.
๋ํ ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ ์ ๋๊ฐ ์ด๋ ํ ํ๋ฆ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ๊ฒ์ธ์ง ์๊ฐํด๋ณด์์ผ ํจ์ ์์๋ค.
์ด ์ ์ด ๋ฏธํกํ๊ธฐ์ ์งํผํฐ์ ํ์ ๋ง์ด ๋น๋ ธ๋ ๊ฒ ๊ฐ๋ค.
์ปดํฌ๋ํธ๋ก ๋๋์ด ์ฝ๋๋ฅผ ์์ฑํ๊ณ props๋ฅผ ํตํด ๊ฐ์ ์ฃผ๊ณ ๋ฐ๋ ๊ฒ๋ ๊ฝค ์ด๋ ต๊ฒ ๋๊ปด์ก๋ค.
์ด๋ฌํ ๋ถ๋ถ์ ๋ค๋ฅธ ์ฌ๋ฌ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด๋ฉฐ ์ตํ ์ ์์ ๊ฒ์ด๋ผ ๋ฏฟ๋๋ค.
๋ค์ ํ๋ก์ ํธ๋ฅผ ์งํํ ๋๋ ์ด๋ฌํ ์ ๋ค์ ๊ธฐ์ตํ๋ฉฐ ๋ ํจ์จ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ํด์ผ๊ฒ ๋ค.
๋!