React native 从redux的外部调用操作

React native 从redux的外部调用操作,react-native,redux,react-redux,React Native,Redux,React Redux,我需要从redux应用程序的外部调用redux操作,就像在外部js文件中一样。我有一个处理REST请求的api.js文件,我所需要的只是在成功响应中调用一个操作,并通过中间件处理操作 问题是,即使我导出我的存储,我也无法访问api.js文件中的存储或分派,因为它没有连接到redux或任何react组件 api.js function ApiRequest(url) { return fetch(thisUrl) .then(async (rawRes) => {

我需要从redux应用程序的外部调用redux操作,就像在外部js文件中一样。我有一个处理REST请求的api.js文件,我所需要的只是在成功响应中调用一个操作,并通过中间件处理操作

问题是,即使我导出我的存储,我也无法访问api.js文件中的存储或分派,因为它没有连接到redux或任何react组件

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)
        });

}

有什么建议吗?

基本上,如果你可以访问商店,你可以在任何时候发送操作

因此,您的问题的解决方案归结为“如何在APIRESQUEST中提供对store变量的访问?有许多解决方案。将
store
分配到
窗口的全局变量是最简单的

但是,全局解决方案最终会出现隐式排序问题(存储必须在API请求之前初始化)以及其他问题

例如,假设您在创建redux应用商店时分配了
window.store=store
,那么您的代码如下所示:

function ApiRequest(url) {
    return fetch(thisUrl)
        .then(async (rawRes) => {
            if (rawRes && rawRes.status === 200) { 
                store.dispatch({type: 'API_SUCCESS_RESPONSE' })`
            }

        })
        .catch((err) => {
            console.log(err)
        });

}
import { createStore } from 'redux'
export apiMiddleware from './apiMiddleware'
const store = createStore(reducers, apiMiddleware)
编辑:要清楚,从您的问题中可以看出,apiMiddleware
不是存储。apiMiddleware是一个函数,给定一个已创建的存储,将其包装在中间件中。程序中的某个地方有如下代码:

function ApiRequest(url) {
    return fetch(thisUrl)
        .then(async (rawRes) => {
            if (rawRes && rawRes.status === 200) { 
                store.dispatch({type: 'API_SUCCESS_RESPONSE' })`
            }

        })
        .catch((err) => {
            console.log(err)
        });

}
import { createStore } from 'redux'
export apiMiddleware from './apiMiddleware'
const store = createStore(reducers, apiMiddleware)

然后^^^这是您要全局导出的存储。

基本上,如果您有权访问该存储,您可以在任何时候分派操作

因此,您的问题的解决方案归结为“我如何提供对APIRESQUEST内部存储变量的访问?”?这个问题有很多解决办法。将
存储
分配给
窗口
的全局变量最简单

但是,全局解决方案最终会出现隐式排序问题(存储必须在API请求之前初始化)以及其他问题

例如,假设您在创建redux应用商店时分配了
window.store=store
,那么您的代码如下所示:

function ApiRequest(url) {
    return fetch(thisUrl)
        .then(async (rawRes) => {
            if (rawRes && rawRes.status === 200) { 
                store.dispatch({type: 'API_SUCCESS_RESPONSE' })`
            }

        })
        .catch((err) => {
            console.log(err)
        });

}
import { createStore } from 'redux'
export apiMiddleware from './apiMiddleware'
const store = createStore(reducers, apiMiddleware)
编辑:要清楚,从您的问题来看,
api
不是商店。ApiMiddleware是一个函数,给定一个已经创建的存储,将其封装在中间件中。程序中的某个地方有如下代码:

function ApiRequest(url) {
    return fetch(thisUrl)
        .then(async (rawRes) => {
            if (rawRes && rawRes.status === 200) { 
                store.dispatch({type: 'API_SUCCESS_RESPONSE' })`
            }

        })
        .catch((err) => {
            console.log(err)
        });

}
import { createStore } from 'redux'
export apiMiddleware from './apiMiddleware'
const store = createStore(reducers, apiMiddleware)

然后^^^这是您要全局导出的商店。

谢谢,这正是我想要的,但不幸的是,我在react native应用程序中使用了redux,因此我认为没有任何窗口变量可供使用,有什么建议吗?请参阅react native中的global是类似的窗口,所以只需要global.dispatch=store.dispatch谢谢,这正是我想要的,但不幸的是,我在react-native应用程序中使用了redux,所以我认为没有任何窗口变量可供使用,有什么建议吗?请参阅react-native中的global是类似的窗口,所以只需要global.dispatch=store.dispatch