假设我有一个不了解Redux的地理位置服务,并且我将其配置为:
backgroundGeoLocation.configure(
callback // will be called with an argument when the coordinates change
);
什么是使服务回调调度Redux操作而不store
从单独的模块导出并使用store.dispatch()
(因为这将是单例)的最干净方法是什么?
如果要将某些值传递给JavaScript中的某些代码,则需要使用函数。
例如,
function createGeoLocationService(store) {
let backgroundGeoLocation = new BackgroundGeoLocation()
backgroundGeoLocation.configure(coordinates => {
store.dispatch({ type: 'UPDATE_COORDINATES', coordinates })
})
return backgroundGeoLocation
}
现在,无论您在哪里创建商店,都可以创建该服务:
let store = createStore(reducer)
let backgroundGeoLocation = createGeoLocationService(store)
如果需要在组件中访问它,则可以:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句