【发布时间】:2021-01-05 16:17:58
【问题描述】:
我正在慢慢地从 Redux 迁移到 Redux 工具包。我还是很新,但我有这个登录操作功能。如何翻译下面的旧功能我需要createAsyncThunk 来实现吗?
export const login = (email, password) => (dispatch) => {
dispatch(requestLogin());
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((user) => {
dispatch(responseLogin(user));
})
.catch((error) => {
dispatch(loginError());
});
};
我的授权切片看起来像这样:
const authSlice = createSlice({
name: "authSlice",
initialState: {
isLoggingIn: false,
isLoggingOut: false,
isVerifying: false,
loginError: false,
logoutError: false,
isAuthenticated: false,
user: {},
},
reducers: {
signInWithEmail: (state, action) => {
const { email, password } = action.payload;
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((response) => {
const {
uid,
email,
emailVerified,
phoneNumber,
password,
displayName,
photoURL,
} = response.user;
})
.catch((error) => {
console.log(error);
});
},
},
extraReducers: {},
});
【问题讨论】:
-
如果您需要在动作创建器中使用
dispatch,可以使用createAsyncThunk。你能在你的项目中使用async/await吗? -
是的,因为马克已经提供了答案。你能告诉我如何调度我的行动吗?
标签: javascript reactjs redux react-redux redux-toolkit