,所以我寫一個陣營,終極版的Web應用程序,我呼叫調度從我的反應的組分是這樣的:從空白的JavaScript文件調用調度功能
this.props.dispatch(someAction());
現在,我需要從javascript調用調度函數不是React組件,那麼我如何導入調度函數並在這種情況下使用它? 謝謝。
,所以我寫一個陣營,終極版的Web應用程序,我呼叫調度從我的反應的組分是這樣的:從空白的JavaScript文件調用調度功能
this.props.dispatch(someAction());
現在,我需要從javascript調用調度函數不是React組件,那麼我如何導入調度函數並在這種情況下使用它? 謝謝。
的dispatch
功能是你的終極版store的成員。如果您創建商店並將其導出到模塊中,則只需將商店導入模塊並調用dispatch
函數即可。
實施例:
// store.js
import { createStore } from 'redux'
export default createStore(reducers)
// somefile.js
import store from './store'
store.dispatch(someAction)
dispatch method是store的方法之一。 react-redux使dispatch
可通過provider
和mapDispatchToProps
作爲道具提供給組件。
您可以從store直接調度:
store.dispatch(action)