假设我有一个不知道Redux的地理定位服务,我将其配置如下:
backgroundGeoLocation.configure(
callback // will be called with an argument when the coordinates change
);
Run Code Online (Sandbox Code Playgroud)
什么是最简单的方法使服务回调调度Redux操作而不store从单独的模块导出并使用store.dispatch()(因为这将是一个单例)?
Dan*_*mov 14
如果要将某些值传递给JavaScript中的某段代码,则需要使用函数.
例如,
function createGeoLocationService(store) {
let backgroundGeoLocation = new BackgroundGeoLocation()
backgroundGeoLocation.configure(coordinates => {
store.dispatch({ type: 'UPDATE_COORDINATES', coordinates })
})
return backgroundGeoLocation
}
Run Code Online (Sandbox Code Playgroud)
现在,无论您在何处创建商店,都要创建该服务:
let store = createStore(reducer)
let backgroundGeoLocation = createGeoLocationService(store)
Run Code Online (Sandbox Code Playgroud)
如果您需要在组件中访问它,您可以:
| 归档时间: |
|
| 查看次数: |
4079 次 |
| 最近记录: |