我需要从我的redux应用的外侧调用redux动作,就像在外部js文件中一样。我有一个处理REST请求的api.js文件,我需要的只是在成功响应中调用一个动作,并由我的中间件执行处理动作。从redux的外侧调用动作
proplem是我无法访问存储或在api.js文件中发送,即使我出口我的商店因为它没有连接到REDX或任何反应组件。
api.js
function ApiRequest(url) {
return fetch(thisUrl)
.then(async (rawRes) => {
if (rawRes && rawRes.status === 200) {
// <------- Here i wanna run redux action
}
})
.catch((err) => {
console.log(err)
});
}
apiMiddleware:
const apiMiddleware = store => next => (action) => {
switch (action.type) {
case 'API_SUCCESS_RESPONSE' :
console.log('action success executed')
break;
default :
}
return next(action);
};
export default apiMiddleware;
什么建议吗?
谢谢你,这正是我想要的,但不幸的是,我在反应原生应用程序中使用了REDX,所以我不这么认为有任何窗口变量可以使用,任何建议? – N3TC4t
查看https://stackoverflow.com/questions/35577551/how-to-use-global-variables-in-react-native/35577584 – AnilRedshift
全球是类似的反应本地窗口,所以所有想要的是global.dispatch = store。调度 – N3TC4t