Calling Dispatch function from a blank javascript file

自作多情 提交于 2019-12-17 16:27:49

问题


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

this.props.dispatch(someAction());

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.


回答1:


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.

Example:

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

export default createStore(reducers)


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

store.dispatch(someAction)



回答2:


The dispatch method is one of the store's methods. react-redux makes dispatch available to components as props via the provider, and mapDispatchToProps.

You can dispatch directly from the store:

store.dispatch(action)


来源:https://stackoverflow.com/questions/45043219/calling-dispatch-function-from-a-blank-javascript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!