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>
);
}
}
From what I understand, I can't possibly connect to store in this component as we are just configuring and pass the store into Provider. How would i possibly dispatch an action in my onLocation
event?
You can directly dispatch using the store object.
store.dispatch({type:"UPDATE_VARIABLE", payload:variable.value})
If you are in some other component where the store object isnt readily available, export it from the main component and import it to a location where it is needed and use the above statement
react-redux has own hooks to call them
const dispatch = useDispatch();
const state = useSelector((state) => state);
do not forget to import them
import { useDispatch, useSelector } from 'react-redux';
vs code will autocomplete if you write useDispatch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With