[React] axios 요청을 성공할 때까지 보내야 할 때
2023. 7. 22. 14:32ㆍReact
다음 코드와 같이 setTimeout을 사용한다.
RETRY_DELAY_MS = 2000 // 2초마다 재송신
const func = () = {
const [ qnaList, setQNAList ] = useState( null );
const fetchData = () => {
const body = {
access_token: cookie.load( 'user' ).accessToken,
history_id: historyId
}
axios.post( "http://localhost:8000/history/qna", body ).then( ( res ) => {
console.log( res.data );
const result = res.data
if ( result.type ) { setQNAList( result.qnas ) }
else { console.log( result.message ); setTimeout( fetchData, RETRY_DELAY_MS ) }
} ).catch( error => {
// 요청 중 에러가 발생했을 때 처리
console.error( error );
} )
}
useEffect( () => {
fetchData();
}, [] )
return
(
)
}
'React' 카테고리의 다른 글
[React] {}을 쓸때 주의점 (0) | 2023.07.25 |
---|---|
[React] npm install "must provide string spec" 오류 날때 (0) | 2023.07.25 |
[React] Error: ENOSPC: System limit for number of file watchers reached (watchers 에러 뜰때) (0) | 2023.07.22 |
[React] response할 때 주의사항 (0) | 2023.07.21 |
[React] 환경 세팅 (0) | 2023.07.20 |