codestates / codestates/underTheSea
[Client] then을 쓰기 싫은데 useEffect를 써야한다면?
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
### 어떤 에러인가요?
- .then으로 이어지는 코드가 보기 싫고 직관적이지 않아서 async와 await을 통해 구현하려고 하였는데 axios요청을 useEffect안에서 하는경우, useEffect안에서의 콜백함수는 async를 사용할 수 없었다.
### 에러 메시지
```bash
React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing
```
### 에러 핸들링 방법
- 외북에서 함수를 aync함수로 선언한뒤에 useEffect안에서 함수를 실행하는 방법을 통해 문제를 해결할 수 있었다.
```js
const getConInfo = async () => {dd
const response = await axios.get(
`http://localhost:80/container/${container_id}/${month}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
{
withCredentials: true,
}
);
// console.log("response:", response.data.data);
localStorage.setItem("conInfo", JSON.stringify(response.data.data));
};
useEffect(() => {
getConInfo();
});
```
### 에러 핸들링을 위해 참고한 레퍼런스 링크
[링크]()
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.