Go๋ฏผ๋ณด๋‹ค Go

ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž

Create

[React] ๋ฆฌํŒฉํ† ๋ง - ๋ฐ์ดํ„ฐ/์•ก์…˜/๊ณ„์‚ฐ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ์‚ฌ์šฉํ•˜์ž

SleepingOff 2024. 5. 8. 13:47

๐Ÿพ๋“ค์–ด๊ฐ€๋ฉฐ

์ด์ „์— ์“ด ์ฝ”๋“œ๋ฅผ ๋Œ์•„๋ณด๋ฉด์„œ ๋ฆฌํŒฉํ† ๋ง์„ ํ•ด๋ณด์•˜๋Š”๋ฐ, ์ •๋ง ๊ณผ๊ฑฐ์˜ ์ €์—๊ฒŒ ์•ˆํƒ€๊นŒ์šด ์‹œ์„ ์ด ๊ฐ‘๋‹ˆ๋‹ค.
๋ฆฌํŒฉํ† ๋งํ•˜๋ฉด์„œ ์–ด๋–ค ํ–‰๋™์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”ํ–ˆ๋Š”์ง€ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ์˜ ์ „๋ฌธ์€ ๋‹ค์Œ ๋งํฌ์— ์˜ฌ๋ ค๋‘์—ˆ์Šต๋‹ˆ๋‹ค: https://gist.github.com/Sleepingoff/8c9494d244374292446f3f1784169c12

โœจ๋ณธ๊ฒฉ์ ์œผ๋กœ

โœ…์–ด๋–ค ์ผ์„ ํ•˜๊ณ  ์žˆ์„๊นŒ?

๋จผ์ € ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์—์„œ ์–ด๋–ค ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

  1. user๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹œ๊ฐ„์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ํ˜„์žฌ ์‹œ๊ฐ„์— ๋งž์ถฐ์„œ day ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ

    • holiday๋ผ๋Š” ๊ณตํœด์ผ ๋ฐ์ดํ„ฐ๋ฅผ ํ† ๋Œ€๋กœ ๊ณตํœด์ผ์ด๋ผ๋ฉด ํ•œ ๋ฒˆ ๋” day ์ƒํƒœ๋ฅผ ์ฒดํฌํ•ด์„œ ์—…๋ฐ์ดํŠธ
  2. ์ƒˆ๋กœ๊ณ ์นจ, ๋‚ ์งœ๋ณ€๊ฒฝ, ์ง€ํ•˜์ฒ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ์— ๋”ฐ๋ผ์„œ ๊ฐ’ ์ดˆ๊ธฐํ™”

    • rowInfo: ์ง€ํ•˜์ฒ  ์ •๋ณด
    • count: ์š”์ฒญ ํšŸ์ˆ˜
  3. ์ง€ํ•˜์ฒ  ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

1๋ฒˆ์˜ ๊ฒฝ์šฐ ์ •๋ง ์ปดํฌ๋„ŒํŠธ์™€ ์ƒ๊ด€์ด ์—†๋Š” ๊ณ„์‚ฐ ๋กœ์ง์ด์—ˆ์Šต๋‹ˆ๋‹ค.
2๋ฒˆ์˜ ๊ฒฝ์šฐ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์™€ ์—ฌ๋Ÿฌ ์ƒํƒœ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋“ค์„ ๊ฐœ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
3๋ฒˆ์˜ ๊ฒฝ์šฐ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋Š” ํŽ˜์ด์ง€๋กœ ์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— api ํ˜ธ์ถœ์„ ํ•˜๋Š” ์ฑ…์ž„์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ผ๋‹จ 3๋ฒˆ์„ ์ œ์™ธํ•˜๊ณ  1๋ฒˆ๊ณผ 2๋ฒˆ์„ ์ง‘์ค‘์ ์œผ๋กœ ๊ฐœ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

โœ…๋ณ€์ˆ˜๋ช…? ์„ ์–ธ? ๋ณ€๊ฒฝํ•˜๊ธฐ

๊ธฐ์กด์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐ€์žฅ ์‹ฌ๊ฐํ–ˆ๋˜ ๊ฒƒ์ด ๋ณ€์ˆ˜๋ช…์ž…๋‹ˆ๋‹ค. ๋ฌด์Šจ ๋œป์ธ์ง€ ํ•œ์ฐธ์„ ๊ณจ๋˜˜ํžˆ ์ƒ๊ฐํ•ด์•ผ ๊ฒจ์šฐ ๊ธฐ์–ต๋‚˜๋Š” ์ด๋ฆ„๋“ค ๋ฟ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ์ด๋ฅผ useState๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ์„ ์–ธํ•œ ๊ฑธ ๋ณด๋‹ˆ ์ฐธ..ใ…Žใ…Ž

๊ธฐ์กด ์ฝ”๋“œ

//rowInfo: [] @description ์•„๋งˆ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ฌ ๋•Œ ํ‚ค๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ ๋ณ€์ˆ˜๋ช…. ์ง€ํ•˜์ฒ ์—ญ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค.
const [rowInfo, setRowInfo] = useState([]);
//reqCount: numer @description ์š”์ฒญํšŸ์ˆ˜. ์™€ ์ง„์งœ ๊ธฐ์–ต ์•ˆ๋‚˜..
const [reqCount, setReqCount] = useState(0);
//count: number @description reqCount๊ฐ€ ๋ฒ”์œ„๋ผ๋ฉด count๋Š” reqCount ๋ฒ”์œ„ ๋‚ด์—์„œ ๋Œ์•„๊ฐ€๋Š” ์ˆซ์ž ์™œ ์ด๋”ด์‹์ด์ง€?
const [count, setCount] = useState(0);
//day: string @description ํ˜„์žฌ ์š”์ผ์— ๋Œ€ํ•ด์„œ ํ‰์ผ, ํ† ์š”์ผ, ๊ณตํœด์ผ์ธ์ง€ ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋‚ด์„œ ๊ตฌ๋ถ„
const [day, setDay] = useState(`1`);
//refresh: boolean @description ์ƒˆ๋กœ๊ณ ์นจ์šฉ ๋ฒ„ํŠผ์„ ์œ„ํ•œ ๊ฐ’. ์ƒˆ๋กœ๊ณ ์นจ: ๋‚จ์€ ์ง€ํ•˜์ฒ  ์‹œ๊ฐ„๋•Œ๋ฌธ์— ํ•„์š”
const [refresh, setRefresh] = useState(false);
//direct: string @description day์™€ ๋น„์Šท. ์ƒํ–‰, ํ•˜ํ–‰, ์ˆœํ™˜ ๋“ฑ์„ ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋‚ด์„œ ๊ตฌ๋ถ„
const [direct, setDirect] = useState('1');
//showTime: boolean @description ๊ธ‰ํ–‰์— ์ด๋ฒคํŠธ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์‹ค์งˆ์ ์œผ๋กœ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง„ ๊ธฐ์–ต ์•ˆ๋‚˜..
const [showTime, setShowTime] = useState(false);

์ฃผ์„์œผ๋กœ ๊ฐ state์— ์–ด๋–ค ์ •๋ณด๊ฐ€ ๋‹ด๊ธฐ๋Š”์ง€ ์ ์–ด๋ณด์•˜๋Š”๋ฐ, ๊ตณ์ด ์ƒํƒœ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค๋„ ์„ž์—ฌ ์žˆ์—ˆ๊ณ , ์ •๋ง ๋งŒ๋“ค๋‹ค๊ฐ€ ๋งŒ ๊ธฐ๋Šฅ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์„ ์–ธํ•œ ์ƒํƒœ๋“ค๋„ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ข€ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌํŒฉํ† ๋ง ์ดํ›„

const [count, setCount] = useState(0);
const [refresh, setRefresh] = useState(false);
const [timeTableInfo, setTimeTableInfo] = useState([]);
//์—๋Ÿฌ์ผ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด state ์ถ”๊ฐ€
const [isError, setIsError] = useState(false);

const ONCE_REQ_LIST_COUNT = 5;
const START_INDEX = ONCE_REQ_LIST_COUNT*count;
const currentDay =  getUserSettingDay();
const currentDirect = getUserDirect();

  //button UI๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€. ํ˜„์žฌ ์„ ํƒํ•œ ๋ฒ„ํŠผ์˜ variant ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์ฃผ๊ธฐ ์œ„ํ•จ.
const isCurrentDay = (number) => currentDay === number + '';
const isCurrentDirect = (number) => currentDirect === number + '';
const makeClickedButton = (boolean) => boolean ? 'primary' : 'basic'

const subTimeParams = {
  startIndex: START_INDEX,
  endIndex: START_INDEX + ONCE_REQ_LIST_COUNT-1,
  stationCD: getUserStationCD(),
  day: currentDay ,
  direct: currentDirect
}
  • ์™ธ๋ถ€ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ: ๊ธฐ์กด์— state๋กœ ์„ ์–ธํ•œ ์นœ๊ตฌ๋“ค์€ ๋ฐ”๋กœ ํŽ˜์ด์ง€์—์„œ ๊ณ„์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ถ€๋ถ„์„ ์™ธ๋ถ€ ํ•จ์ˆ˜๋กœ ์„ ์–ธ
  • ๋ฐ”๋กœ ๊ฐ์ฒด๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ: state ๊ด€๋ฆฌ๊ฐ€ ๊ตณ์ด ํ•„์š”์—†๊ณ , ๋ Œ๋”๋งํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์„ ์ƒˆ๋กœ ํ• ๋‹นํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.
  • ํ•œ๋ฒˆ์— ๋ถˆ๋Ÿฌ์˜ฌ ๋ฆฌ์ŠคํŠธ ๊ฐœ์ˆ˜๋Š” ๊ณ ์ •๋œ ๊ฐ’์ด๋‹ˆ ์ƒ์ˆ˜ํ˜•ํƒœ๋กœ ์„ ์–ธ

โœ…๊ณ„์‚ฐ ๋กœ์ง ์™ธ๋ถ€ ํ•จ์ˆ˜๋กœ ์„ ์–ธ

user์˜ ํ˜„์žฌ ์‹œ๊ฐ„์„ ์•Œ์•„๋‚ด๊ณ , ํ•ด๋‹น ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ง€ํ•˜์ฒ  ์‹œ๊ฐ„ํ‘œ๋ฅผ ํ‰์ผ, ๊ณตํœด์ผ, ํ† ์š”์ผ ๋“ฑ์˜ ๊ธฐ์ค€์œผ๋กœ ๋ณด์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ

    useEffect(()=>{
        setRowInfo([]);
        setCount(0);
    },[id])

    //TODO: ์‚ฌ์šฉ์ž ์œ„์น˜, ์‹œ๊ฐ„์— ๋งž๊ฒŒ ์‹œ๊ฐ„ํ‘œ ์กฐํšŒํ•˜๊ธฐ
    useEffect(()=>{
        const userDate = new Date();
        const today = userDate.getDay();// 0: ์ผ์š”์ผ ~ 6: ํ† ์š”์ผ
        const date = userDate.getDate();
        const month = userDate.getMonth() + 1;
        const year = userDate.getFullYear();

        switch(today){
            case 6:
                setDay('2');
                break;
            case 0:
                setDay('3');
                break;
            default:
                setDay('1');
                break;
        }

        if(holiday.holiday.includes(`${year}-${month}-${date}`)){
            setDay(3);
        }
        setDirect('1');
    }, [refresh])

๊ธฐ์กด์— ์žˆ๋˜ ๊ณ„์‚ฐ ๋กœ์ง๋“ค ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐํ™” ์‹œ ์‹คํ–‰ํ•˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ํ•„์š”์—†๋Š” useEffect๋ฅผ ์‚ฌ์šฉ: ์ดˆ๊ธฐํ™”๋“ , ๋‹ค๋ฅธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋˜๋“  ๊ณ„์‚ฐ ๋กœ์ง์€ ์‹คํ–‰๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋งˆ ์—ฌ๊ธฐ์—์„œ ์ข€ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š์•˜๋‚˜ ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

  • ์—ฌ๋Ÿฌ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ: swtich๋„ ๊ทธ๋ ‡๊ณ , if๋ฌธ๋„ ๊ทธ๋ ‡๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด day ๊ฐ’๋งŒ ๋ฐ”๋€Œ๋Š” ์ค„ ์•Œ์•˜๋”๋‹ˆ ๋œฌ๊ธˆ์—†์ด ๋งˆ์ง€๋ง‰ ์ค„์— setDirect๋กœ direct ๊ฐ’๋„ ๋ณ€๊ฒฝํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌํŒฉํ† ๋ง ์ดํ›„

  const initUserDay = () => {
    let INIT_USER_DAY = getUserToday();
    searchParams.set('day', INIT_USER_DAY)
  }

  //์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์—์„œ setUserDay ์‚ฌ์šฉ
  const setUserDay = (string) => {
    searchParams.set('day', string)
  }
  const getUserSettingDay = () => {
    return searchParams.get('day')
  }
  const getUserToday = () => {
    const userDate = new Date();
    const today = userDate.getDay();// 0: ์ผ์š”์ผ ~ 6: ํ† ์š”์ผ
    const date = userDate.getDate();
    const month = userDate.getMonth() + 1;
    const year = userDate.getFullYear();

    const isHoliday = holiday.holiday.includes(`${year}-${month}-${date}`);

    if( isHoliday || today === 0) return `3`
    else if(today === 6) return `2`
    else return `1`
  }

๋ณ„๋„๋กœ ํ›…์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. useSearchParams ํ›…์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์ „์—ญ ์ƒํƒœ์ธ url ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง ์‚ฌ์šฉ: ํ•˜๋‚˜ ํ•˜๋‚˜ state๋กœ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹จ ์ „์—ญ ์ƒํƒœ์—์„œ ๊บผ๋‚ด์˜ค๋Š” ๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ–ˆ์Šต๋‹ˆ๋‹ค.
  • init, set, get ๋“ฑ์œผ๋กœ ์—ญํ•  ๋ถ„๋ฆฌ: ์ดˆ๊ธฐ๊ฐ’์€ getUserToday๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด์ง€๋งŒ, ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๊ฐ’์œผ๋กœ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒ˜์Œ ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์‚ฌ์šฉํ•  ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง, ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋กœ์ง, ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋กœ์ง์œผ๋กœ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

โœ…์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ •๋ฆฌ

๊ธฐ์กด ์ฝ”๋“œ

const handleClickRefresh = () => { setRefresh(!refresh);}
const handleClickDay = (event) => {setDay(event.target.id);}
const handleClickDirect = (event) =>{setDirect(event.target.id || "1");}

์ตœ๊ทผ์— ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ returnํ•˜๋ฉด ์‚ฌ์šฉ์ด ํŽธํ•œ ๊ฑธ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌํŒฉํ† ๋ง ์ดํ›„

const handleClickRefresh = (refresh) => () => { setRefresh(refresh) }
const handleClickDay = (id) => (event) => { setUserDay(id) }
const handleClickDirect = (id) => (event) =>{ setUserDirect(id) }
const handleClickMore = () => {setCount( prev => prev + 1 )}
  • () => (event) => {} ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ
  • setState: ๊ธฐ์กด state๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋“ค์˜ ๋ณ€๊ฒฝ์œผ๋กœ ํ•ด๋‹น ๋ถ€๋ถ„๋„ ๊ต์ฒด
  • clickMore: count๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•œ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์„ ์–ธ

โœ…return ๋ถ€๋ถ„ ์ •๋ฆฌ

์œ„์˜ ๋ฆฌํŒฉํ† ๋ง ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด์„œ return ๋ถ€๋ถ„์—๋„ ๋งŽ์€ ๋ณ€ํ™”๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ

    return(
        <PageTemplate>
            <StyledLinkBtn onClick={handleClickStation}>{state}</StyledLinkBtn>
            <ButtonWrap>
                <DayButton onClick={handleClickDay}>
                    <DefaultBtn variant={day === "1" ? "primary" :"basic"} id='1'>ํ‰์ผ</DefaultBtn>
                    <DefaultBtn variant={day === "2" ? "primary" :"basic"} id='2'>ํ† ์š”์ผ</DefaultBtn>
                    <DefaultBtn variant={day === "3" ? "primary" :"basic"} id='3'>๊ณตํœด์ผ</DefaultBtn>
                </DayButton>
                <DirectButton onClick={handleClickDirect}>
                    <DefaultBtn variant={"secondary"} onClick={handleClickRefresh}>์ดˆ๊ธฐํ™”</DefaultBtn>
                    <DefaultBtn variant={direct === "1" ? "primary" :"basic"} id='1'>์ƒํ–‰</DefaultBtn>
                    <DefaultBtn variant={direct === '2' ? "primary" :"basic"} id='2'>ํ•˜ํ–‰</DefaultBtn>
                    <DefaultBtn variant={"basic"} onClick={()=>setShowTime(!showTime)}>๊ธ‰ํ–‰</DefaultBtn>                
                </DirectButton>
            </ButtonWrap>
            <ul>
                {rowInfo.map((info, index)=>{
                    return (
                    <StyledList key={index}>
                        <Time>{info[0]}</Time>
                        <Station>{info[2]} โžก๏ธ {info[3]}</Station>
                        <StyledTag aria-label="์ง€ํ•˜์ฒ  ๋ฒˆํ˜ธ">{info[1]}</StyledTag>
                    </StyledList>
                    )
                })}
            </ul>
        </PageTemplate>
    )

๋ฆฌํŒฉํ† ๋ง ์ดํ›„

  return(
    <PageTemplate>
        <StyledLinkBtn onClick={handleClickStation}>{state}</StyledLinkBtn>
        <ButtonWrap>
            <DefaultBtn variant={"secondary"} onClick={handleClickRefresh(!refresh)}>์ดˆ๊ธฐํ™”</DefaultBtn>
            <DayButton>
                ['ํ‰์ผ', 'ํ† ์š”์ผ', '๊ณตํœด์ผ'].map((day, index) => (
                  <DefaultBtn key={index} variant={makeClickedButton(isCurrentDay(index+1))} onClick={handleClickDay(`${index+1}`)}>{day}</DefaultBtn>
                ))
            </DayButton>
            <DirectButton>
                ['์ƒํ–‰', 'ํ•˜ํ–‰'].map((direct, index) => (
                  <DefaultBtn key={index} variant={makeClickedButton(isCurrentDirect(index+1))} onClick={handleClickDirect(`${index+1}`)}>{direct}</DefaultBtn>
                )) 
            </DirectButton>
        </ButtonWrap>
        {isError ?  
          <ErrorComp/>
        : <ul>
            {timeTableInfo.map((info)=>(
                <StyledList key={info.id}>
                    <Time>{info.ARRIVETIME}</Time>
                    <Station>{info.SUBWAYSNAME} โžก๏ธ {info.SUBWAYENAME}</Station>
                    <StyledTag aria-label="์ง€ํ•˜์ฒ  ๋ฒˆํ˜ธ">{info.TRAIN_NO}</StyledTag>
                </StyledList>
            ))}
        </ul>

        }
        <DefaultBtn variant={"secondary"} onClick={handleClikMore}>๋” ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</DefaultBtn>
    </PageTemplate>
  )
  • button๋“ค ์ •๋ฆฌ: ๊ธฐ์กด์—๋Š” ์ข€ ๋” ์ •๋ณด๋ฅผ ์ž˜ ๋ณด์ด๊ธฐ ์œ„ํ•ด์„œ ํ’€์–ด์„œ ์ผ๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” ์ข€ ๋ฌถ์–ด์„œ ์ค‘๋ณต๋œ ์ฝ”๋“œ๋ฅผ ์ค„์—ฌ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ถ€๋ถ„์— ๋Œ€ํ•ด์„  ์ •๋‹ต์ด ์—†์Šต๋‹ˆ๋‹ค. ์กฐ๋งŒ๊ฐ„ DayButton๊ณผ DirectButton ์ปดํฌ๋„ŒํŠธ๋„ ์ถ”์ƒํ™”ํ•ด์„œ ์ค„์–ด๋“ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • timeTableInfo: ๊ธฐ์กด์— ๋ฐฐ์—ด ๋‚ด ๋ฐฐ์—ด๋กœ ์ •๋ณด๋ฅผ ๋„ฃ์–ด์„œ ํŠน์ • ์ธ๋ฑ์Šค๋กœ ๋ถˆ์–ด์™”๋‹ค๋ฉด, ๊ฐ์ฒด๋กœ ๋„ฃ์–ด์„œ ์–ด๋–ค ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”์ง€ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค.

โœ”๏ธ๊ฒฐ๋ก 

๋ถ€์ˆ˜ ํšจ๊ณผ๋ฅผ ๋ฐœ์ƒํ•˜๋Š” ์•ก์…˜๊ณผ ์ˆœ์ˆ˜ ๊ณ„์‚ฐ ์œ„์ฃผ๋กœ ํ•˜์—ฌ ๋ถ€์ˆ˜ ํšจ๊ณผ๊ฐ€ ์—†๋Š” ๊ณ„์‚ฐ์ด๋ผ๋Š” ์—ญํ• ๋กœ ๋ถ„๋ฆฌ๋ฅผ ํ•˜๋ฉด์„œ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋” ์†์„ ๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์— ๋ถˆ๋Ÿฌ์˜ค๋Š” useSubTime ์ค‘์—์„œ๋„ day๋งŒ ๋˜ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ์•„์ง ๋” ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๋ณธ ์ ์€ ์—†์ง€๋งŒ, ๊ธฐ์ค€์ด ์ •ํ•ด์ง€๊ณ  ๋ฆฌํŒฉํ† ๋ง์„ ํ•˜๋‹ˆ ๋„ˆ๋ฌด ์žฌ๋ฐŒ๋„ค์š”... ๊ณผ๊ฑฐ์˜ ๋‚˜์•ผ ๋”ฑ ๊ธฐ๋‹ค๋ ค!!

728x90