๐Ÿœ Mine ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ์ ํŠธ - ๋‹ฌ๋ ฅ ํŽ˜์ด์ง€ (1)

Darleneยท2021๋…„ 8์›” 23์ผ
0

Mine ํ”„๋กœ์ ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
8/12

๐Ÿœ Mine ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ์ ํŠธ - ๋‹ฌ๋ ฅ ํŽ˜์ด์ง€

์˜ค๋Š˜์€ ๋‹ฌ๋ ฅ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์œ„ ์บก์ณํ•œ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ œ๊ฐ€ ์›ํ•˜๋Š” ๋‹ฌ๋ ฅ ๋ชจ์Šต์ด ๋‚˜์˜ค๊ธฐ๊นŒ์ง€ ์ˆ˜ ๋งŽ์€ ์‹œํ–‰์ฐฉ์˜ค๊ฐ€ ์žˆ์—ˆ๋‹ต๋‹ˆ๋‹ค. ์ €์—๊ฒŒ ๊ฐ€์žฅ ์–ด๋ ค์› ๋˜ ๋ถ€๋ถ„์ธ๋ฐ์š”.
๋‹ฌ๋ ฅ์ด ์ œ์ผ ํฐ ๋‚œ๊ด€์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ.. ๊ณ„์† ์ถ”๊ฐ€์ ์œผ๋กœ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค๋ฉด์„œ ํ˜„์žฌ ์ง๋ฉดํ•˜๊ฒŒ ๋œ ๋ฌธ์ œ๋“ค์ด ๋ชจ๋‘ ์–ด๋ ต๊ธด ๋งˆ์ฐฌ๊ฐ€์ง€๋„ค์š”.

๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์‚ด์ง๋งŒ ๋ณด๋ฉด ์™„๋ฒฝํ•˜๊ฒŒ ์™„์„ฑ๋˜์–ด ๋ณด์ด์ง€๋งŒ ์•„์ง ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ถ€๋ถ„์€ ์ถ”ํ›„ ํ•ด๊ฒฐํ•ด๋ณด๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ์ด๋ ‡๊ฒŒ ๋‹ฌ๋ ฅ ๋งŒ๋“œ๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์ ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

โ˜‘๏ธ ๋‹ฌ๋ ฅ์„ ๊ทธ๋ ค๋‚ด๋ด„
โ˜‘๏ธ ๋ฐ์ดํ„ฐ ์Šคํ‚ค๋งˆ ๋ฌธ์ œ์  ๋ฐœ๊ฒฌ์œผ๋กœ ๋‹ฌ๋ ฅ์„ ๋‹ค์‹œ ๊ทธ๋ ค๋‚ด๋ด„
โ˜‘๏ธ ํ† ์š”์ผ, ์ผ์š”์ผ ์ƒ‰๊น” ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ
โ˜‘๏ธ ์˜ค๋Š˜ ๋‚ ์งœ ํ‘œ์‹œํ•ด์ฃผ๊ธฐ
โ˜‘๏ธ ๋ณด์—ฌ์ง€๋Š” ๋‹ฌ๋ ฅ์—์„œ ์ด์ „ ๋‹ฌ & ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ ํ๋ฆฌ๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์•„์ง ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„๊ณผ ๋ฌธ์ œ์ ์ด ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ํ•ด๊ฒฐ ๊ณผ์ •๋„ ์ƒ์„ธํžˆ ์ ์–ด๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

๐Ÿ”ฒ ๋ถˆ๋ณ€์„ฑ ๊นจํŠธ๋ฆฌ์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝ ํ•„์š”!
๐Ÿ”ฒ ์˜ค๋Š˜ ๋‚ ์งœ๊ฐ€ ์ฃผ๋ง์ด๋ฉด ์ƒ‰๊น”์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ์  ๋ฐœ๊ฒฌ
๐Ÿ”ฒ ํ˜„์žฌ ๋‹ฌ ์‹œ์ž‘์ผ์ด ์ผ์š”์ผ์ด๋ฉด ์ด์ „ ๋‹ฌ ๋ฐ์ดํ„ฐ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์Œ


๐Ÿ““ ๋‹ฌ๋ ฅ ๋งŒ๋“ค๊ธฐ ์ „ ์ค€๋น„ ๋‹จ๊ณ„

1) ๋‹ฌ๋ ฅ์„ ์–ด๋–ป๊ฒŒ ๊ทธ๋ ค๋‚ผ๊ฒƒ์ธ๊ฐ€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ

๋‹ฌ๋ ฅ์„ ๊ทธ๋ ค๋‚ด๋Š” ๊ฒƒ์ด ๋ง‰์—ฐํ–ˆ๊ธฐ์— ์šฐ์„  ๊ตฌ๊ธ€๋ง์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
"์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋‹ฌ๋ ฅ ๋งŒ๋“ค๊ธฐ"

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ 2๊ฐœ๋ฅผ ์ฐพ์•„์„œ ์ฐธ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ1 : https://bigtop.tistory.com/66
์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ2 : https://velog.io/@mygomi/React-%EB%9D%BC%EC%9D%B4%EB%B8%8C%EB%9F%AC%EB%A6%AC%EC%97%86%EC%9D%B4-%EC%BA%98%EB%A6%B0%EB%8D%94-%EA%B5%AC%ED%98%84

๋‹ฌ๋ ฅ์„ ์–ด๋–ป๊ฒŒ ๊ทธ๋ ค๋‚ผ์ง€์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๊ณ ๋ฏผ 1๏ธโƒฃ
ํ•ด๋‹น ์›” ๋‚ ์งœ๋งŒ ๊ทธ๋ ค๋‚ธ๋‹ค.

๊ณ ๋ฏผ 2๏ธโƒฃ
ํ•ด๋‹น ์›” ๋‚ ์งœ ์™€ ์ด์ „ ๋‹ฌ, ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ๋ฅผ ๊ทธ๋ ค๋‚ธ๋‹ค.

2) ๋‹ฌ๋ ฅ ํ…Œ์ด๋ธ”์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด ๋‚ผ๊ฒƒ์ธ๊ฐ€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ

์–ด๋–ป๊ฒŒ CSS๋ฅผ ์ ์šฉํ•ด์„œ ๋ฐ•์Šคํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ๋‚ผ์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์†์œผ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋ฉด 7๊ฐœ์˜ ์นธ์•ˆ์— ๊ฐ๊ฐ ์š”์ผ๊ณผ ๋‚ ์งœ๋ฅผ ๊ทธ๋ ค๋‚ด๋Š” ๊ฒƒ์€ ์‰ฌ์šด ์ผ์ด์ง€๋งŒ ์ฝ”๋“œ๋กœ ์–ด๋–ป๊ฒŒ ํ’€์–ด๋‚ด์•ผํ• ์ง€ ๋ง‰์—ฐํžˆ ์–ด๋ ต๊ฒŒ๋งŒ ๋Š๊ปด์ง€๋„ค์š”.

๊ทธ๋ž˜์„œ ์•„์ฃผ ์ž‘๊ฒŒ ์ž‘๊ฒŒ ์ชผ๊ฐœ์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ž‘์€ ๋ธ”๋Ÿญ์œผ๋กœ ๋งŒ๋“ค์–ด ๊ทธ ๋ธ”๋Ÿญ๋“ค์„ ์กฐ๋ฆฝํ•˜์—ฌ ์™„์„ฑ๋ณธ์œผ๋กœ ๋งŒ๋“ค๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ํ•˜๋Š” ์ƒ๊ฐ์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

(๋‹ฌ๋ ฅ ํ…Œ์ด๋ธ” = 7 x ( 1 + 4 ๋˜๋Š” 5 ) )
โžก๏ธ (์š”์ผ ํ…Œ์ด๋ธ” = 7 x 1 ) + (๋‚ ์งœ ํ…Œ์ด๋ธ” = 7 x 4 ๋˜๋Š” 5 )
โžก๏ธ (์š”์ผ ํ…Œ์ด๋ธ” = 7 x 1 ) + {(์ฃผ ํ…Œ์ด๋ธ” = 7 x 1) x 4 ๋˜๋Š” 5 }

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์š”์ผ์ปดํฌ๋„ŒํŠธ์™€ ๋‚ ์งœ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์กฐ๋ฆฝํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

<Calendar>
  <CalendarDays />
  <CalendarMonth />
</Calendar>

๊ทธ๋ฆฌ๊ณ  ์ฒซ ๋ฒˆ์งธ ๊ณ ๋ฏผ์˜ ๊ฒฐ์ •์œผ๋กœ "2๏ธโƒฃ ํ•ด๋‹น ์›” ๋‚ ์งœ ์™€ ์ด์ „ ๋‹ฌ, ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ๋ฅผ ๊ทธ๋ ค๋‚ธ๋‹ค." ๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค.

3) ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ

๋‚ ์งœ๋ฅผ ๊ทธ๋ ค๋‚ด๋ ค๋ฉด Date ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
์ฝ”๋”ฉ ์ž…๋ฌธํ•  ๋•Œ JavaScript Date ๊ฐ์ฒด ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ์ด์ œ์„œ์•ผ Date ๊ฐ์ฒด ๋ฅผ ๊ณต๋ถ€ํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ–๊ฒŒ ๋˜์—ˆ๋„ค์š”.

Date ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ์„ ํƒํ•œ ๋‹ฌ๋ ฅ์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ previousDates
2๏ธโƒฃ thisDates
3๏ธโƒฃ nextDates

์ด์ „ ๋‹ฌ ๋‚ ์งœ์™€, ํ˜„์žฌ ๋‹ฌ ๋‚ ์งœ์™€ ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ๋“ค์„ ๋‹ด์„ ๊ทธ๋ฆ‡์„ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋„ค์š”. ์ด ๋ฐฐ์—ด๋“ค์„ ํ•ฉ์ฒดํ•ด์ฃผ๋ฉด ํ˜„์žฌ ๋ณด์—ฌ์ง€๋Š” ๋‹ฌ๋ ฅ์—์„œ ํ•„์š”ํ•œ ๋‚ ์งœ๋“ค์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”.

const previousDates = [];
const thisDates = [];
const nextDates = [];

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ ๊ทธ๋ฆ‡์— ๋‹ด์•„๋‚ด์•ผ ํ•  date๋“ค์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ €๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด 4๊ฐ€์ง€ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„๋‚ผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

1๏ธโƒฃ lastDateOfPreviousMonth (์ด์ „ ๋‹ฌ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ)
2๏ธโƒฃ lastDayOfPreviousMonth (์ด์ „ ๋‹ฌ ๋งˆ์ง€๋ง‰ ์š”์ผ)
3๏ธโƒฃ lastDateOfThisMonth (์ด๋ฒˆ ๋‹ฌ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ)
4๏ธโƒฃ lastDayOfThisMonth (์ด๋ฒˆ ๋‹ฌ ๋งˆ์ง€๋ง‰ ์š”์ผ)

๐Ÿ““ ๋‹ฌ๋ ฅ์„ ๊ทธ๋ ค๋ณด์ž!!!

์ฐธ๊ณ ๋กœ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ๋ฆฌ๋•์Šค ํˆดํ‚ท์„ ์‚ฌ์šฉํ•œ๋‹ค.

์ด์ „ ํŽธ : ๐Ÿœ Mine ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋กœ์ ํŠธ - ์˜ˆ์‚ฐ ํŽ˜์ด์ง€

์ด์ „ ํŽธ์—์„œ๋Š” "๋ฆฌ์•กํŠธ, ๋ฆฌ๋•์Šค๋ฅผ ์ด์šฉํ•ด์„œ input๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ณผ์ • ์†Œ๊ฐœ"๋กœ ๋ฆฌ์•กํŠธ์—์„œ๋Š” useState๋กœ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ , ๋ฆฌ๋•์Šค์—์„œ๋Š” ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ๋ฆฌ๋“€์„œ์—์„œํ•˜๊ณ  ์•ก์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋‹ค๋ฃจ์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํŽธ์—์„œ๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ๋ฆฌ๋•์Šค ํˆดํ‚ท์„ ์‚ฌ์šฉํ•ด์„œ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๊ณผ์ •์„ ๋‹ค๋ฃฐ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ createSlice ๋ฆฌ๋•์Šค ํˆดํ‚ท์„ ์‚ฌ์šฉํ•ด์„œ ์ €์žฅํ•  ๋ฐ์ดํ„ฐ์™€ ์•ก์…˜์„ ์ •์˜ํ•˜์ž.

๋ฆฌ๋•์Šค์—์„œ ์ƒํƒœ ๊ด€๋ฆฌ๋Š” reducer.js ์™€ actions.js ํŒŒ์ผ์—์„œ ๊ด€๋ฆฌํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ์•ก์…˜๊ณผ ๋ฆฌ๋“€์„œ์— ๋Œ€ํ•œ ๊ด€๋ฆฌํ•˜ ๋ณต์žกํ•ด์ง€๊ฒ ์ฃ ?

slice.js ํŒŒ์ผ์—์„œ ๋ฆฌ๋“€์„œ์™€ ์•ก์…˜์„ ํ•œ๊บผ๋ฒˆ์— ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ๋•์Šค ํˆดํ‚ท์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

[1] slice.js ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

์•„๋ž˜์™€ ๊ฐ™์ด slice.js ํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์™€ ์•ก์…˜์„ ์ •์˜ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

import { createSlice } from '@reduxjs/toolkit';

const { actions, reducer } = createSlice({
  name: '',
  initialState: {},
  reducers: {},
});

export {
  actions,
  reducer,  
};

[2] ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ธฐ

๋‹ฌ๋ ฅ์„ ๊ทธ๋ฆด๋•Œ year๊ณผ month๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์›” ์ด๋™์„ ํ•  ๋•Œ๋„ ํ•„์š”ํ• ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

import { createSlice } from '@reduxjs/toolkit';

const { actions, reducer } = createSlice({
  name: 'application',
  initialState: {
    year: 2021,
    month: 7,
  },
  reducers: {},
});

export const {} = actions;

export default reducer;

๐Ÿ“Œ ๋‹ฌ๋ ฅ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์ž.

[1] CalendarPage ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

์ €๋Š” ์ตœ๋Œ€ํ•œ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

import CalendarContainer from './CalendarContainer';

export default function CalendarPage() {
  return (
    <>
      <CalendarContainer />
    </>
  );
}

[2] CalendarContainer ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

์ปจํ…Œ์ด๋„ˆ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
๋ฆฌ๋•์Šค(์ค‘์•™ ์ƒํƒœ ์ €์žฅ์†Œ)์—์„œ state๋ฅผ ๊บผ๋‚ด ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  CalendarMonth ์ปดํฌ๋„ŒํŠธ์— props๋กœ ๋„˜๊ฒจ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

import { useSelector } from 'react-redux';

import CalendarDays from './CalendarDays';
import CalendarDates from './CalendarDates';

export default function Calendar() {
  const { year, month } = useSelector((state) => ({
    year: state.year,
    month: state.month,
  }));
 
  return (
    <>
      <CalendarDays />
      <CalendarMonth
        year={year}
        month={month}  
      />
    </>
  );
}

[3] CalendarDays ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ œ๊ฐ€ ์ƒ๊ฐํ•  ๋•Œ ์—ฌ๊ธฐ์„œ days๋Š” ๋ฆฌ๋•์Šค์— ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์„œ useState๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตณ์ด useState๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•ด์ค˜๋„ ๋ฉ๋‹ˆ๋‹ค.

 const days = ['์ผ', '์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† '];

๊ทธ๋ฆฌ๊ณ  map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์š”์ผ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

import React, { useState } from 'react';

export default function CalendarDays() {
  const [days] = useState(['์ผ', '์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† ']);

  return (
    <>
      <div>
        {
          days.map((day) => (
            <Day
              key={day}
            >
              {day}
            </Day>
          ))
        }
      </div>
    </>
  );
}

[4] CalendarMonth ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

export default function CalendarMonth({ year, month  }) {
  // Date๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•ด์„œ "์ด์ „ ๋‹ฌ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ"์™€ "์ด์ „ ๋‹ฌ ๋งˆ์ง€๋ง‰ ์š”์ผ" ๊ตฌํ•˜๊ธฐ
  const lastDateOfPreviousMonth = new Date(year, month - 1, 0).getDate();
  const lastDayOfPreviousMonth = new Date(year, month - 1, 0).getDay();
  
  // Date๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•ด์„œ "์ด๋ฒˆ ๋‹ฌ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ"์™€ "์ด๋ฒˆ ๋‹ฌ ๋งˆ์ง€๋ง‰ ์š”์ผ" ๊ตฌํ•˜๊ธฐ
  const lastDateOfThisMonth = new Date(year, month, 0).getDate();
  const lastDayOfThisMonth = new Date(year, month, 0).getDay();

  // ์ด์ „ ๋‹ฌ ๋‚ ์งœ, ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ๋ฅผ ๊ตฌํ•ด์„œ ๋‹ด์„ ๊ทธ๋ฆ‡์„ ์ •์˜ํ•ด์ค€๋‹ค.
  // ์ด๋ฒˆ ๋‹ฌ ๋‚ ์งœ๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.
  const previousDates = [];
  const thisDates = [...Array(lastDateOfThisMonth + 1).keys()].slice(1);
  const nextDates = [];

  // previousDates๋ฅผ ์ฑ„์›Œ์ค๋‹ˆ๋‹ค.
  if (lastDayOfPreviousMonth !== 6) {
    for (let i = 0; i < lastDayOfPreviousMonth + 1; i++) { 
   previousDates.unshift(lastDateOfPreviousMonth - i);
    }
  }
  
  // nextDates๋ฅผ ์ฑ„์›Œ์ค๋‹ˆ๋‹ค.
  for (let i = 1; i < 7 - lastDayOfThisMonth; i++) {
    nextDates.push(i);
  }

  // ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ๋‹ฌ, ์ด๋ฒˆ ๋‹ฌ, ๋‹ค์Œ ๋‹ฌ์„ ํ•ฉ์ณ์ค๋‹ˆ๋‹ค.
  const dates = previousDates.concat(thisDates, nextDates);

  // ๊ทธ๋ฆฌ๊ณ  weeks ํ…Œ์ด๋ธ”๋กœ ๊ฐ๊ฐ date๋ฅผ ๋‚˜๋ˆ„์–ด ๋‹ด์Šต๋‹ˆ๋‹ค.
  const weeks = [];

  for (let i = 0; i <= 35; i += 7) {
    weeks.push([...dates].slice(i, i + 7));
  }

  // ๋งˆ์ง€๋ง‰์œผ๋กœ map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์ถœ๋ ฅํ•ด๋ƒ…๋‹ˆ๋‹ค.
  return weeks.map((week) => (
    <div
      key={week}
    >
      {
        week.map((date) => (
          <div
            key={date}
          >
            {date}
          </div>
        ))
      }
    </div>
  ));
}

css๋กœ ์ด๋ ‡๊ฒŒ ๋‹ฌ๋ ฅ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๋ฉด
์•„๋ž˜์™€ ๊ฐ™์ด 2021๋…„ 7์›”์„ ๊ทธ๋ ค๋‚ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ํ•ด์•ผํ•  ๊ธฐ๋Šฅ 1. ํ† ์š”์ผ, ์ผ์š”์ผ ์ƒ‰๊น” ๊ตฌ๋ถ„ํ•ด์ฃผ๊ธฐ
์ถ”๊ฐ€ ํ•ด์•ผํ•  ๊ธฐ๋Šฅ 2. ์˜ค๋Š˜ ๋‚ ์งœ ํ‘œ์‹œํ•ด์ฃผ๊ธฐ
์ถ”๊ฐ€ ํ•ด์•ผํ•  ๊ธฐ๋Šฅ 3. ๋ณด์—ฌ์ง€๋Š” ๋‹ฌ๋ ฅ์—์„œ ์ด์ „ ๋‹ฌ & ๋‹ค์Œ ๋‹ฌ ๋‚ ์งœ ํ๋ฆฌ๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ

์ด๋ ‡๊ฒŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ์˜ˆ์œ ๋‹ฌ๋ ฅ์ด ๊ทธ๋ ค์งˆ ๊ฒƒ ๊ฐ™๋„ค์š”:)

๋‹ค์Œ ํŽธ์—์„œ๋Š” ์›” ์ด๋™ํ•˜๋Š” ๊ธฐ๋Šฅ๊ณผ ๋ฐ์ดํ„ฐ ์Šคํ‚ค๋งˆ ๋ฌธ์ œ์  ๋ฐœ๊ฒฌ์œผ๋กœ ๋‹ฌ๋ ฅ์„ ๋‹ค์‹œ ๊ทธ๋ ค๋ ค๋‚ด๋Š” ๊ณผ์ •๊ณผ ์ •์ ์œผ๋กœ ์ •์˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

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