날짜 : 2024-02-07 15:19

코드

<ProfileComponent name={historyDetail.doctorName} intro={historyDetail.department}/>

API에서 받아온 historyDetail는 정상적인 데이터가 나온다고 확신할 수 없기 때문에 (null값이 나올 수 있다.) Component에 컴파일 되기 전에 에러가 뜨게된다.

{historyDetail &&  
<ProfileComponent name={historyDetail.doctorName} intro={historyDetail.department}/>  
}

위 처럼 조건부 렌더링을 사용하게 되면 데이터가 로드되기 전에 컴포넌트가 렌더링되는 것을 방지할 수 있다.