本文介绍了从空白的 javascript 文件调用 Dispatch 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个 React-Redux 网络应用程序,我像这样从我的 React 组件调用调度:

So i'm writing a React-Redux web app, and i call dispatch from my react components like this :

this.props.dispatch(someAction());

现在我需要从一个不是 React 组件的 javascript 函数调用 dispatch,那么我如何导入 dispatch 函数并在这种情况下使用它?谢谢.

Now i need to call dispatch from a javascript function that is not a React Component, so how do i import the dispatch function and use it in this case ?Thank you.

推荐答案

dispatch 函数是你的 redux store.如果您在模块中创建和导出商店,则就像在模块中导入商店并调用 dispatch 函数一样简单.

The dispatch function is a member of your redux store. If you created and exported your store in a module, it would be as easy as importing the store in your module and calling the dispatch function.

示例:

// store.js
import { createStore } from 'redux'

export default createStore(reducers)


// somefile.js
import store from './store'

store.dispatch(someAction)

这篇关于从空白的 javascript 文件调用 Dispatch 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:52