export default class App extends Component {
...
componentDidMount() {
//registering event listener
BackgroundGeolocation.on('location', this.onLocation, this.onError);
}
onLocation(location) {
//wishing to dispatch an action to update variable in store
}
render() {
return (
<Provider store={store}>
<AlertProvider>
<MainNavigator screenProps={{ isConnected: this.state.isConnected }} />
</AlertProvider>
</Provider>
);
}
}
据我了解,由于我们只是在配置存储并将其传递给Provider,因此我可能无法连接到此组件中的存储。我如何在onLocation事件中调度动作?
答案 0 :(得分:9)
您可以使用商店对象直接调度。
store.dispatch({type:"UPDATE_VARIABLE", payload:variable.value})
如果您在不容易找到商店对象的其他组件中,请从主组件中将其导出,然后导入到需要它的位置,并使用上面的语句
答案 1 :(得分:0)
您可以使用此代码并将构造函数添加到此类
constructor(props) {
super(props);
}
onLocation() {
this.props.dispatch({type: GET_THINGS_REQUESTED, payload: value});
}