Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- owlcarousel
- npm start
- window 정책변경
- 웹아이콘
- 이미지반응형
- 글자들여쓰기
- npm install 문제
- 동적객체
- slickslider
- 의존성문제
- vscode git clone
- Git clone
- maxwidth
- npm install
- react npm install
- package.json
- 플러그인
- fontawesome
- node설치
- node 오류
- git lab clone
- 그누보드반응형
- 정적객체
- 단어단위로떨어지기
- legacy-peer
- 아이콘사용법
- minwidth
- MediaQuery
- XEIcon
- googleicon
Archives
- Today
- Total
어쩌다 알게 된 ƪ(•̃͡•̃͡ ƪ
컴포넌트와 함수 선언 순서, 실행 순서_코드리뷰 본문
728x90
저번에 함수형 컴포넌트와 선언형 컴포넌트에서 선언 순서에 따라 실행여부가 달라지는 걸 보고
내 코드를 봤는데 의문이 들었따🤔❓
현재 코드
function CommonCalendar({
date,
events,
localizer,
toolbar = false,
onNavigate,
eventPropGetter
}){
const month = getMonth(date) + 1;
const year = getYear(date);
return(
<>
<div className="tit_box">
<Button
className='border-light btn_prev'
color='dark'
isOutline
icon='ChevronLeft'
aria-label='Prev'
onClick={btnPrevMonth}
/>
<div className="month_date">
{formatByLang(date, "LLLL yyyy", language)}
</div>
<Button
className='border-light btn_next'
color='dark'
isOutline
icon='ChevronRight'
aria-label='Next'
onClick={btnNextMonth}
/>
</div>
<div className="calendar_box">
<Calendar
localizer={localizer}
events={events}
date={date}
views={["month"]}
toolbar={toolbar}
eventPropGetter={eventPropGetter}
startAccessor="start"
endAccessor="end"
onNavigate={onNavigate}
components={{
event: CustomEvent,
header: CustomHeader,
dateCellWrapper: (props) => <CustomDayCell {...props} month={month} year={year} />,
dateHeader: (props) => <CustomDayCellHeaderContent {...props} month={month} year={year} />,
}}
selectable
resizable
/>
</div>
</>
)
};
// custom navigate
const [currentDate, setCurrentDate] = useState(new Date());
const handleNavigate = (date, view, action) => {
setCurrentDate(date);
}
const btnPrevMonth = () => {
setCurrentDate(prev => addMonths(prev, -1));
}
const btnNextMonth = () => {
setCurrentDate(prev => addMonths(prev, 1));
}
이것만 봤을 때는 실행이 안되어야 하는 코드인데 알고보니
저 CommonCalendar 컴포넌트 호을 밑에서 해주기 때문에 가능한 것
=> “선언 순서가 아니라 실행 순서” 때문에 작동 가능
return(
<CommonCalendar
localizer={localizer}
date={currentDate}
events={events}
onNavigate={handleNavigate}
/>
)
👌
CommonCalendar 컴포넌트는 함수 선언이기 때문에 호이스팅으로 위로 끌어올려지지만,
실제로 함수가 실행되는 시점은 return ( <CommonCalendar .../> )이 호출될 때이며,
그 시점은 btnPrevMonth 등이 const로 모두 초기화된 후이기 때문!
❌ 안되는 경우 예시
function CommonCalendar() {
btnPrevMonth(); // ❌ 실행 시점이 아니라 선언 시점에 접근하기 때문에 불가
}
const btnPrevMonth = () => {}
🔖🔖🔖정리
** function은 메뉴판에 등록된 요리 이름
** const 함수는 요리사가 실제로 재료를 준비하는 단계
- 메뉴판에 요리 이름(CommonCalendar)이 먼저 등록됨
- 요리사(btnPrevMonth 등)가 나중에 재료를 준비함
- 마지막에 손님(React 렌더링)이 요리를 주문함 → 그때는 이미 재료 준비 완료
화살표 함수 컴포넌트, 함수 선언문 컴포넌트 형태와 사용 차이
✅ ① 화살표 함수 컴포넌트 ( React 컴포넌트 ⭕, 커스텀 훅 ⭕, 일반 유틸 함수 ⭕)const Test = () => { return Test;};장점일관된 스코프(lexical this) → class 기반 this 문제 없음재정의 방지 가능 (const라 재
dazzle-bini.tistory.com
728x90
'개발 > 🟦 React' 카테고리의 다른 글
| <Portal>이란? _ 보통 모달을 많이 씀 (0) | 2025.11.26 |
|---|---|
| 컴포넌트의 옵션 props 기본 지정_컴포넌트에 지정하면 안되나? (1) | 2025.11.25 |
| Calendar 컴포넌트_ import 'react-big-calendar' (0) | 2025.11.19 |
| 화살표 함수 컴포넌트, 함수 선언문 컴포넌트 형태와 사용 차이 (0) | 2025.11.14 |
| [CRA/Plain React SPA] 다국어 설정 i18n.js (0) | 2025.11.14 |
Comments
