reactjs - 如何将 thunk 动作转换为 saga 动作
问题描述
我有以下使用 redux-thunk 登录用户的操作。
export const loginUser = userData => dispatch => {
Axios.post('/users/login', userData)
.then( res => {
// retrieve token from the response
const token = res.data.token;
// console.log(token);
// pass the token in session
sessionStorage.setItem("jwtToken", token);
// set the auth token
setAuthToken(token);
// decode the auth token
const decoded = jwt_decode(token);
// pass the decoded token
dispatch(setCurrentUser(decoded))
})
.catch(err => {
if(err.response.data){
console.log(err.response)
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
}
})
}
export const setCurrentUser = (decoded, dispatch) => {
return{
type:SET_CURRENT_USER,
payload:decoded,
}
}
我将如何将此动作转换为传奇函数/动作?
目前后端抛出错误,
错误的请求
可能是因为它没有获取正确的数据或其他东西。
这是我的尝试
动作.js
export const userLogin = userData => dispatch => ({
type: SET_CURRENT_USER,
userData
})
export const userLogInSuccess = data => ({
type: USER_LOG_IN_SUCCESS,
data
})
export const userLogInFailure = error => ({
type: USER_LOG_IN_FAILURE,
error
})
export const setCurrentUser = (decoded, dispatch) => {
return{
type:SET_CURRENT_USER,
payload:decoded,
}
}
传奇/auth.js
import api from '../api';
import { SET_CURRENT_USER, GET_ERRORS, GET_CURRENT_USER} from '../actions/types';
import {userLogInSuccess, setCurrentUser, userLogInFailure} from '../actions/authActions';
export function* userLogin(action){
try{
const user = yield call(api.user.loginUser, action.data);
const token = user.token
console.log(token);
// pass the token in session
sessionStorage.setItem("jwtToken", token);
setAuthToken(token);
const decoded = jwt_decode(token);
// pass the decoded token
// dispatch(setCurrentUser(decoded))
yield put(userLogInSuccess(user));
yield put( setCurrentUser(decoded))
}
catch(error){
yield put(userLogInFailure(error.response.data));
}
}
export function* watchUserLogIn() {
yield takeLatest(SET_CURRENT_USER, userLogin);
}
export default function* () {
yield fork(watchUserLogIn);
}
api
import Axios from './Axios';
export default {
user:{
loginUser: userData =>
Axios.post('/users/login',userData).then(res => res.data.token)
}
};
登录组件
.....
handleSubmit = (e) => {
e.preventDefault();
const {formData} = this.state;
const {username, password} = formData;
const creds = {
username,
password
}
this.props.userLogin(creds);
// console.log(creds);
}
....
const mapStateToProps = (state) => ({
auth: state.auth
})
const mapDispatchToProps = (dispatch) => ({
userLogin: (userData) => dispatch(userLogin(userData)),
})
export default connect(mapStateToProps, mapDispatchToProps)(Login)
解决方案
在 Network 选项卡中检查您的有效负载,如果已调用请求,则表示您的 saga 函数中的操作已传递到 api。
推荐阅读
- javascript - 在网站上嵌入私有 Google Drive 图像
- android - Android Emulator 无响应 V4.2.1
- php - 如何从存储在远程 FTP 服务器上的文件在 Lavarel 中创建 ZIP 下载?
- javascript - 动态滑动器只能在第一张幻灯片上显示和循环
- python - 我想将一个函数绑定到 tkinter 中的一个标签,这让我很困惑
- php - 无法从 Windows Azure VM 上的 PHP 访问映射的 Azure 文件共享
- excel - 如何使用另一个宏停止自动刷新单元格的宏?
- javascript - 将 curl 响应数据从 php 发送到 javascript 以呈现 html 或直接在 php 中执行 html 内容
- kylin - Apache Kylin 多维数据集构建导致错误:Java 堆空间
- html - Facebook Open Graph 不工作/显示为空白页