:모든 요청에 적용될 구성 기본(Config Defaults) 값을 지정할 수 있습니다.
axios.defaults.baseURL = 'http://localhost:4000';
: 이벤트를 취소할 수 있는 경우, 이벤트의 전파를 막지않고 그 이벤트를 취소합니다 (mdn)
: 입력 받은 회원가입 정보를 요청 바디에 담아 전송하기 위해 fetchSignUp이라는 thunk를 만듭니다.
export const fetchSignUp = createAsyncThunk(
'signUp/fetchSignUp',
async (form) => {
const { nickname, email, password } = form;
const response = await axios.post('http://localhost:4000/auth/sign-up', { nickname, email, password });
return response.data;
}
)
: 유효성 검사가 모두 통과된 경우, submit 버튼을 누르면, dispatch 메소드를 사용하여 thunk를 실행한다.
const handleSubmit = (e) => {
e.preventDefault();
if(nicknameMessage === ""
&& emailMessage === ""
&& passwordMessage === ""
&& confirmPasswordMessage === "") {
dispatch(fetchSignUp(joinInfo));
}
setJoinInfo({
nickname: '',
email: '',
password: '',
confirmPassword: ''
})
const initialState = {
user: null,
userError: null,
isSignUp: false,
isLogin: false,
loading: false,
}
: 객체에 reducer함수들을 제공할 수 있고 이를 기반으로 액션 타입문자열과 액션 생성자 함수를 자동으로 생성합니다.
[출처] soyoung210.github.io - Introducing: createSlice
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {},
extraReducers: {
// 회원가입
[fetchSignUp.pending]: (state) => {
state.loading = true;
},
[fetchSignUp.fulfilled]: (state) => {
state.loading = false;
state.isSignUp = true;
},
[fetchSignUp.rejected]: (state, { payload }) => {
state.loading = false;
state.userError = payload;
},
// 로그인
[fetchLogin.pending]: (state) => {
state.loading = true;
},
[fetchLogin.fulfilled]: (state, { payload }) => {
state.loading = false;
state.user = payload;
state.isLogin = true;
},
[fetchLogin.rejected]: (state, { payload }) => {
state.loading = false;
state.userError = payload;
},
}
})
useEffect(() => {
if(isSignUp){
history.push('/login');
} else {
setServerErrorMessage(userError);
}
},[history, isSignUp, userError])
이처럼 매 번 새롭게 컴포넌트가 렌더링될 때 Effect Hook이 실행됩니다.
const handleSubmit = (e) => {
e.preventDefault();
if(emailMessage === "" && passwordMessage === "") {
dispatch(fetchLogin(loginInfo));
}
setLoginInfo({
email: '',
password: '',
})
};
export const fetchLogin = createAsyncThunk(
'login/fetchLogin',
async (form) => {
const { email, password } = form;
const response = await axios.post('http://localhost:4000/auth/log-in', { email, password });
return response.data;
}
)
const initialState = {
user: null,
userError: null,
isSignUp: false,
isLogin: false,
loading: false,
}
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {},
extraReducers: {
// 회원가입
[fetchSignUp.pending]: (state) => {
state.loading = true;
},
[fetchSignUp.fulfilled]: (state) => {
state.loading = false;
state.isSignUp = true;
},
[fetchSignUp.rejected]: (state, { payload }) => {
state.loading = false;
state.userError = payload;
},
// 로그인
[fetchLogin.pending]: (state) => {
state.loading = true;
},
[fetchLogin.fulfilled]: (state, { payload }) => {
state.loading = false;
state.user = payload;
state.isLogin = true;
},
[fetchLogin.rejected]: (state, { payload }) => {
state.loading = false;
state.userError = payload;
},
}
})
useEffect(() => {
if(user){
history.push('/');
}
if(userError){
setServerErrorMessage(userError);
}
}, [history, user, userError])
개발자 도구 > 애플리케이션 > 쿠키