Calling Dispatch function from a blank javascript

2019-01-26 14:31发布

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.

2条回答
Lonely孤独者°
2楼-- · 2019-01-26 15:13

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)
查看更多
趁早两清
3楼-- · 2019-01-26 15:29

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)
查看更多
登录 后发表回答