How can i dispatch a redux action from code which is outside of react class?
store.dispatch is exposed since you create the store yourself using redux.
If your setup looks like this
import { createStore } from 'redux'
import { Provider } from 'react-redux'
let store = createStore(/* reducers */)
<Provider store={store}>
/* your app here */
</Provider>
then you can call store.dispatch anywhere you have store available.
This.
Also you can also grab dispatch as a prop from your connected components and pass it anywhere you like.
is this a valid approach? I mean call the exposed store from a module that is outside of react-class? should be always try to get this by props ?
Yes, you can always access the store from outside of React. The instance of the store is the same between the two (this.props.store === global.store), so it's perfectly safe and valid.
Most helpful comment
Yes, you can always access the store from outside of React. The instance of the store is the same between the two (
this.props.store === global.store), so it's perfectly safe and valid.