fetch 혹은 axios를 통해 서버로부터 데이터를 보내거나 가져올 수 있다.
이때 데이터를 요청하는 동안 발생하는 로딩시간동안의 UI, 에러가 발생했을때의 UI를 만들어 보자.
const [loading, setLoading] = useState(false);
const [error, setError] = useState(undefined);
useEffect(()=>{
setLoading(true);
setError(undefined);
});
.catch((e) => {
setError('에러 발생');
}
.finally(() => { setLoading(false) })
if(loading) return <p>Loading...</p>
if(error) return <p>{error}</p>
(로딩, 에러 발생시 보여줄 UI는 컴포넌트로 따로 만들어주면 더 좋고)
😢발생한 문제점
-> checkbox input 안에 속성으로 checked={checked} 를 넣어줘서 해결함
(체크 여부 상태를 저장한 checked라는 state를 넘겨준것임)