我有一个helper.js文件,其中包含所有辅助函数,包括带有 axios 的 HTTP 请求处理程序。这是我的helper.js文件代码:
const HTTPRequest = (path, body, method = 'POST', authorizedToken = null) => {
return new Promise((resolve, reject) => {
let headers = {
'Content-type': 'multipart/form-data',
};
// Set authorization token
if (authorizedToken) {
headers['Authorization'] = "JWT " + authorizedToken; // Here i want to get user token from the redux store not though passing the token
}
const fulHeaderBody = {
method,
headers,
timeout: 20,
url: path
};
axios(fulHeaderBody).then(response => {
if (response.success) {
resolve(response);
} else {
// Show toast about the error
Toast.show({
text: response.message ? response.message : 'Something went wrong.',
buttonText: 'Okay',
type: 'danger'
});
resolve(response);
}
}).catch(error => {
if (error.response) {
if(error.response.status === 401){
// I WANT TO DISPATCH AN ACTION HERE TO LOGOUT CLEAR ALL STORAGE DATA IN REDUX AND CHANGE THE STATE TO INITIAL
}else{
console.log('Error', error.message);
}
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
reject(error);
})
});
};
export default HTTPRequest;
现在,我面临的问题是,我如何从这个辅助函数中分派一个动作,或者我如何user token从 redux 存储中获取。
并helper.js像这样调用它
内部HTTPRequest功能
helloWorld();
我只能看到日志Hello world has been dispatched,但我没有看到调度员的任何日志。
谁能告诉我如何处理这个问题。?
三国纷争
牛魔王的故事
相关分类