๐Ÿ“’ ๋‹ฌ๋ ฅ ์ƒํƒœ๊ด€๋ฆฌํ•˜๊ธฐ

์†์œค์ฃผยท2022๋…„ 7์›” 22์ผ
2
post-thumbnail
post-custom-banner

react-big-calendar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ปค์Šคํ…€ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ฌ๋ ฅ์„ 4๊ฐœ ์ปดํฌ๋„ŒํŠธ๋กœ ์ชผ๊ฐฐ๋‹ค.

AccountCalendar.js - ๋‹ฌ๋ ฅ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ

ToolBar.js - ์ด์ „ ๋‹ฌ, ๋‹ค์Œ ๋‹ฌ ๋ฒ„ํŠผ ๋‹ฌ๋ ฅ ํ—ค๋” ์ปดํฌ๋„ŒํŠธ

Day.js - ๋‚ ์งœ ์ปดํฌ๋„ŒํŠธ

Event.js - ๋“ฑ๋ก๋œ ์ˆ˜์ž…, ์ง€์ถœ ๋‚ด์—ญ style์„ ์ปค์Šคํ…€ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ


๐Ÿค”ย ๋ฌธ์ œ์ƒํ™ฉ

ToolBar ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‹ค์Œ ๋‹ฌ ํ˜น์€ ์ด์ „ ๋‹ฌ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ,
์–ด๋–ป๊ฒŒํ•˜๋ฉด ๋‹ฌ๋ ฅ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์›”๋ณ„ ๋‚ด์—ญ์„ ์ƒˆ๋กœ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์„๊นŒ?

์ „์ œ์กฐ๊ฑด A

react-big-calendar ์˜ ๊ฒฝ์šฐ ์ชผ๊ฐœ์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฝ์ž…ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
์ž์‹์ปดํฌ๋„ŒํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์—†๋‹ค.

<Calendar
        components={{
          toolbar: ToolBar,
          month: {
            dateHeader: Day,
          },
        }}
      />

์ „์ œ์กฐ๊ฑด B

ToolBar ์ปดํฌ๋„ŒํŠธ์—์„œ ์ด์ „๋‹ฌ, ๋‹ค์Œ๋‹ฌ ํด๋ฆญ ์‹œ ๋™์ž‘ํ•˜๋Š” ๋‚ด์žฅํ•จ์ˆ˜ navigate.bind(null, "PREV")๋Š” onClick์„ ํ™”์‚ดํ‘œํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๊ฒฝ์šฐ ๋™์ž‘ํ•˜์ง€ ์•Š์•„ onClick ์•ˆ์— month state๋ฅผ setํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค.

// ์ด์ „ ๋‹ฌ ๋ฒ„ํŠผ
<Btn type="button" onClick={navigate.bind(null, "PREV")}>

// ๋‹ค์Œ ๋‹ฌ ๋ฒ„ํŠผ
<Btn type="button" onClick={navigate.bind(null, "NEXT")}>

๐Ÿ”ฅย ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1๏ธโƒฃย ToolBar ์ด์ „๋‹ฌ, ๋‹ค์Œ๋‹ฌ onClick navigate.bind(null, "PREV") ํ•จ์ˆ˜๋ฅผ ๋ฐ–์œผ๋กœ ๊บผ๋‚ด click event ์ƒํƒœ๊ด€๋ฆฌํ•˜๊ธฐ

// ToolBar.js
const [click, setClick] = useState(false);

const navigate = (action) => {
    props.onNavigate(action);
    if (!click) setClick(true);
    else setClick(false);
  };

2๏ธโƒฃย ToolBar.js์—์„œ useEffect ํ›… dependency array ์— click ๊ฐ’ ๋„ฃ๊ณ , ์ด์ „ ๋‹ฌ, ๋‹ค์Œ ๋‹ฌ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋งˆ๋‹ค yearMonth ๊ฐ’ redux๋กœ dispatch

// ToolBar.js

const month = moment(date).format("MM");
  const year = moment(date).format("YYYY");
  const YYMM = { month: month, year: year };

useEffect(() => {
    dispatch(getYearMonthDB(YYMM));
  }, [click]);

3๏ธโƒฃย ๋‹ฌ๋ ฅ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ redux์— ์ €์žฅ๋œ yearMonth ๊ฐ’ useSelector๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„ yearMonth ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์›”๋ณ„ ๋‚ด์—ญ ์ƒˆ๋กœ ์š”์ฒญ

// AccountCalendar.js

const yearMonth = useSelector((state) => state.account.yearMonth);

useEffect(() => {
    dispatch(getYearMonthDB(YYMM));
  }, [click]);

4๏ธโƒฃย ๋‹ฌ๋ ฅ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒˆ๋กœ ์กฐํšŒํ•œ ์›”๋ณ„ ๋‚ด์—ญ์„ useSelector๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„ events ์— data ์ „๋‹ฌ

// AccountCalendar.js

const accountList = useSelector((state) => state.account.accountList);

return (
<Calendar
        events={convertCalendarData(accountList)}
        localizer={localizer}
        style={{ height: 100 + "%", width: 100 + "%" }}
        components={{
          toolbar: ToolBar,
          month: {
            dateHeader: Day,
          },
        }}
        onSelectEvent={(eventInfo) => {
          setEventInfo(eventInfo);
          toggleModal();
        }}
        eventPropGetter={Event}
      />
)

์ด๋ฅผ ํ†ตํ•ด ์ตœ์ข…์ ์œผ๋กœ ๋‹ฌ๋ ฅ ์ตœ์ƒ์œ„์ปดํฌ๋„ŒํŠธ์—์„œ ๋”ฑ ํ•œ๋ฒˆ ํ˜„์žฌ ์›”๋ณ„ ๋‚ด์—ญ api์š”์ฒญ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค!

๐Ÿ‘‡ย ํ•ด๊ฒฐ์— ๋„์›€์„ ์ค€ ์ž๋ฃŒ

๐Ÿง‘โ€๐Ÿ’ป react-use-effect-ํ›…-์ดํŽ™ํŠธ-ํ•จ์ˆ˜-ํด๋ฆฐ์—…-ํ•จ์ˆ˜
๐Ÿง‘โ€๐Ÿ’ป react-big-calendar Toolbar ์ปค์Šคํ…€ํ•˜๊ธฐ

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€