- ์๊ฐ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ฒํผํ์ฑํ or ๋นํ์ฑํ ์ปค์คํ ํ ์์ ์ฝ๋
- 1. ์ํ ์ด๊ธฐํ
- 2. useEffect Hooks
- 3. ํ์ฌ ์๊ฐ๊ณผ ์์ผ ๊ฐ์ ธ์ค๊ธฐ
- 4. ๊ณตํด์ผ ํ๋จ ํจ์
- 5. ๋ฒํผ ํ์ฑ ์ฌ๋ถ ๊ฒฐ์
- 6. ์ฃผ๊ธฐ์ ์ผ๋ก ๊ฐฑ์
- 7. ๋ง์ดํธ ํด์ ์ interval ์ ๋ฆฌ
- 8. ์ต์ข ๋ฐํ
- ๐ ์ค์ ์ฌ์ฉ ์์
์๊ฐ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ฒํผํ์ฑํ or ๋นํ์ฑํ ์ปค์คํ ํ ์์ ์ฝ๋
import {useState, useEffect} from "react";
export const useTimeRestrictedToggle = () => {
const [isEnabled, setIsEnabled] = useState(false);
useEffect(()=>{
const checkIsHoliday = (date: Date) => {
const month = date.getMonth() + 1;
const day = date.getDate();
// ๊ณตํด์ผ (์ ์ , ์ค๋ , ์ถ์, ๋ฑ)
const isNewYear = month === 1 && day === 1;
const isLunarHolidy = month === 2 && day === 29;
const isChuseok = month === 10 && [6,7,8].includes(day);
return isNewYear || isLunarHolidy || isChuseok;
}
const getCurrentTime = () => {
const now = new Date();
const hours = now.getHours(); // ํ์ฌ ์ (0~23)
const dayOfWeek = now.getDay(); // ์์ผ (0: ์ผ์์ผ, 6: ํ ์์ผ)
const isHoliday = checkIsHoliday(now); // ํด์ผ ์ธ์ง ํ์ธ
return {hours, dayOfWeek, isHoliday}
}
const evaluateToggle = () => {
const {hours, dayOfWeek, isHoliday} = getCurrentTime();
const isWithinBuinessHours = hours >=9 && hours <18;
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
setIsEnabled(isWithinBuinessHours && !isWeekend && !isHoliday);
};
evaluateToggle(); // ์ด๊ธฐ ์ํ ์ค์
const interval = setInterval(evaluateToggle, 60000); // ๋งค 1๋ถ๋ง๋ค ์ฌํ์ธ
return () => clearInterval(interval); // ์ธ๋ง์ดํธ ์ ํด๋ฆฐ์
},[]);
return isEnabled;
}
์กฐ๊ฑด์ ์๋๊ณผ ๊ฐ์ต๋๋ค .
- ํ์ผ์๋ง ๊ฐ๋ฅ(์ฃผ๋ง โ)
- ์ค์ 9์ ~ ์คํ 6์ ์ฌ์ด๋ง ๊ฐ๋ฅ
- ํน์ ๊ณตํด์ผ๋ ์ ์ธ(์: ์ ์ , ์ค, ์ถ์ ๋ฑ)
๐ ์ค์๊ฐ์ผ๋ก ์ํ๋ฅผ ์ฒดํฌํ๋ ค๋ฉด?
JavaScript์ setInterval()์ ์ฌ์ฉํด 1๋ถ๋ง๋ค ํ์ฌ ์๊ฐ์ ์ฒดํฌํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ React์์๋ useEffect() ์์์ interval()์ ๋ฑ๋กํ๊ณ , ์ปดํฌ๋ํธ๊ฐ ์ธ๋ง์ดํธ๋๋ฉด clearInterval()๋ก ๊ผญ ์ ๋ฆฌํด์ผํฉ๋๋ค.

1. ์ํ ์ด๊ธฐํ
const [isEnabled, setIsEnabled] = useState(false);
๐ ๋ฒํผ ํ์ฑ ์ฌ๋ถ๋ฅผ ์ ์ฅํ๋ ์ํ ๋ณ์
์ฒ์์๋ ๊ธฐ๋ณธ๊ฐ false(๋นํ์ฑ ์ํ)
2. useEffect Hooks
useEffect(() => {
// ์คํํ ์ฝ๋
},[]);
๐ ์ปดํฌ๋ํธ๊ฐ ์ฒ์ ๋ง์ดํธ๋ ๋ ์คํ๋๋ ์ฝ๋ ๋ธ๋ญ
์์ฒ๋ผ []๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฃ์ผ๋ฉด,
- ํด๋น ์ฝ๋๋ ์ปดํฌ๋ํธ๊ฐ ์ฒ์ ํ๋ฉด์ ๋ํ๋ ๋๋ง 1๋ฒ ์คํ ๋ฉ๋๋ค.
๊ทธ๋์ ๊ทธ ์์ setInterval์ ๋ฃ์ผ๋ฉด ์ปดํฌ๋ํธ ๊ธฐ์ค์ผ๋ก ์์๋๊ณ ๋ฐ๋๋ก, ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์์ ์ฌ๋ผ์ง ๋๋ ์ ๋ฆฌ๋ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
3. ํ์ฌ ์๊ฐ๊ณผ ์์ผ ๊ฐ์ ธ์ค๊ธฐ
const getCurrentTime = () => {
const now = new Date();
const hours = now.getHours(); // ํ์ฌ ์ (0~23)
const dayOfWeek = now.getDay(); // ์์ผ (0: ์ผ์์ผ, 6: ํ ์์ผ)
const isHoliday = checkIsHoliday(now); // ํด์ผ ์ธ์ง ํ์ธ
return {hours, dayOfWeek, isHoliday}
}
๐ ํ์ฌ ์๊ฐ์ ๊ฐ์ ธ์ค๊ณ ,
๐ ์์ผ๋ ๊ฐ์ ธ์ค๊ณ ,
๐ ๊ณตํด์ผ์ธ์ง๋ ํ์ธํฉ๋๋ค.
4. ๊ณตํด์ผ ํ๋จ ํจ์
const checkIsHoliday = (date: Date) => {
const month = date.getMonth() + 1;
const day = date.getDate();
// ๊ณตํด์ผ (์ ์ , ์ค๋ , ์ถ์, ๋ฑ)
const isNewYear = month === 1 && day === 1;
const isLunarHolidy = month === 2 && day === 29;
const isChuseok = month === 10 && [6,7,8].includes(day);
return isNewYear || isLunarHolidy || isChuseok
}
2025๋ ๊ธฐ์ค์ผ๋ก,
- 1์ 1์ผ โ ์ ์
- 1์ 29์ผ โ ์ค๋
- 10์ 6์ผ, 7์ผ, 8์ผ โ ์ถ์
์ด ๋ ์ง๋ฉด ๊ณตํด์ผ(true)๋ก ๊ฐ์ฃผ
5. ๋ฒํผ ํ์ฑ ์ฌ๋ถ ๊ฒฐ์
const evaluateToggle = () => {
const {hours, dayOfWeek, isHoliday} = getCurrentTime();
const isWithinBuinessHours = hours >=9 && hours <18;
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
setIsEnabled(isWithinBuinessHours && !isWeekend && !isHoliday);
}
๐ ์กฐ๊ฑด:
- ์๊ฐ์ ์ค์ 9์๋ถํฐ ์ ๋ 6์ ์ ๊น์ง๋ง ๊ฐ๋ฅ(9 <= hour < 18 )
- ์ฃผ๋ง(ํ , ์ผ) โ
- ๊ณตํด์ผ โ
์กฐ๊ฑด์ด ๋ชจ๋ ๋ง์กฑ๋๋ฉด true, ์๋๋ฉด false
6. ์ฃผ๊ธฐ์ ์ผ๋ก ๊ฐฑ์
const interval = setInterval(evaluateToggle, 60000); // ๋งค 1๋ถ๋ง๋ค ์ฌํ์ธ
๐ ๋งค 60์ด๋ง๋ค ๋ค์ ๊ฒ์ฌํด์ ์ค์๊ฐ์ผ๋ก ๋ฒํผ ์ํ๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.
7. ๋ง์ดํธ ํด์ ์ interval ์ ๋ฆฌ
return () => clearInterval(interval); // ์ธ๋ง์ดํธ ์ ํด๋ฆฐ์
๐ ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์์ ์ฌ๋ผ์ง๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐฉ์ง๋ฅผ ์ํด interval ์ ๊ฑฐ๋ฅผ ํฉ๋๋ค.
8. ์ต์ข ๋ฐํ
return isEnabled;
๐ ์ด hook์ ์ฌ์ฉํ ๊ณณ์์๋ true ๋๋ false ๊ฐ์ ๋ฐ์์ ์์ต๋๋ค.
๐ ์ค์ ์ฌ์ฉ ์์
const ๊ฐ๋ฅ์๊ฐ = useTimeRestrictedToggle();
return (
<button disabled={!๊ฐ๋ฅ์๊ฐ}>์ ์ฒญํ๊ธฐ</button>
)
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] ์ฑ๋ฅ ์ต์ ํํ๋ ํ useMemo (0) | 2025.04.09 |
---|---|
[React] Vite + Yarn Berry ํ๋ก์ ํธ ์ค์ (0) | 2025.02.23 |
[react] ERROR in ./src/reportWebVitals.js ํจํค์ง ๋๋ฝ (5) | 2024.12.22 |
[React] SPA ์ฑ๊ธํ์ด์ง ์ ํ๋ฆฌ์ผ์ด์ ์ด๋? ๋ฌด์์ผ๊น. (4) | 2024.09.24 |