createAsyncThunk로 일부러 지연시켜서 pending상태 만드는법 질문
387
작성한 질문수 36
const wait = (timeToDelay) =>
new Promise((resolve) => setTimeout(resolve, timeToDelay));
export const loginAction = createAsyncThunk(LOG_IN, async (data) => {
await wait(1000);
return data;
});
export const logoutAction = createAsyncThunk(LOG_OUT, async () => {
return;
});const userSlice = createSlice({
name: "user",
initialState,
// exraReducers 그냥 reducer는 user/login 이런식으로 액션 이름을 적어야 하지만 extraReducers를 사용하면 login으로 줄일 수 있다
extraReducers: (builder) =>
builder
.addCase([HYDRATE], (state, action) => ({
...state,
...action.payload.user,
}))
.addCase(loginAction.pending, (state) => {
state.isLoggingIn = true;
state.isLoggedIn = false;
})
.addCase(loginAction.fulfilled, (state, action) => {
state.isLoggingIn = false;
state.isLoggedIn = true;
state.user = dummyUser;
state.loginData = action.data;
})
.addCase(loginAction.rejected, (state) => {
state.isLoggingIn = true;
})
.addCase(logoutAction.pending, (state) => {
state.isLoggingOut = true;
state.isLoggedIn = true;
})
.addCase(logoutAction.fulfilled, (state) => {
state.isLoggingOut = false;
state.isLoggedIn = false;
state.user = null;
})
.addCase(logoutAction.rejected, (state) => {
state.isLoggingOut = false;
})
.addCase(signUp.fulfilled, (state, action) => {
state.signUpData = action.data;
})
.addDefaultCase((state) => state),
});제가 redux toolkit으로 제로초님이 만드시는 코드를 만들고 있는데 createAsyncThunk로 일부러 지연 시켜서 pending상태로 만들려고 했는데 이렇게 코드를 짜니까 pending이 제대로 동작을 안하고 로딩 아이콘이 제대로 보이지 않습니다 제가 추가적으로 실험을 해보았는데 state.isLoggingIn을 pending과 fulfilled두개 모두 state.isLoggingIn = true;로하고 동작을 시켜도 로그인 아이콘이 로딩이 안됩니다 하지만 로그아웃은 모두 true로 하니까 로딩이 보입니다...
그래서 액션을 reducer를 작성할때 뭔가를 실수한것 같습니다 무었인지 알 수 있을까요?
그리고 saga의 delay처럼 createAsyncThunk의 지연시켜서 pending상태가 나오는 방법을 알고싶습니다
넥스트 버젼 질문
0
78
2
로그인시 401 Unauthorized 오류가 뜹니다
0
90
1
무한 스크롤 중 스크롤 튐 현상
0
177
1
특정 페이지 접근을 막고 싶을 때
0
103
2
createGlobalStyle의 위치와 영향범위
0
97
2
인라인 스타일 리렌더링 관련
0
92
2
vsc 에서 npm init 설치시 오류
0
148
2
nextjs 15버전 사용 가능할까요?
0
160
1
화면 새로고침 문의
0
123
1
RTK에서 draft, state 차이가 있나요?
0
154
2
Next 14 사용해도 될까요?
0
452
1
next, node 버전 / 폴더 구조 질문 드립니다.
0
349
1
url 오류 질문있습니다
0
211
1
ssh xxxxx로 우분투에 들어가려니까 port 22: Connection timed out
0
375
1
sudo certbot --nginx 에러
0
1281
2
Minified React error 콘솔에러 (hydrate)
0
470
1
카카오 공유했을 때 이전에 작성했던 글이 나오는 버그
0
247
1
프론트서버 배포 후 EADDRINUSE에러 발생
0
329
1
npm run build 에러
0
519
1
front 서버 npm run build 중에 발생한 에러들
0
383
1
서버 실행하고 브라우저로 들어갔을때 404에러
0
338
2
css 서버사이드 랜더링이 적용되지 않아서 문의 드립니다.
0
289
1
팔로워 3명씩 불러오고 데이터 합쳐주는걸로 바꾸고 서버요청을 무한으로하고있습니다.
0
240
2
해시태그 검색에서 throttle에 관해 질문있습니다.
0
202
1





