react-redux icon indicating copy to clipboard operation
react-redux copied to clipboard

Expose store.dispatch?

Open teosz opened this issue 10 years ago • 4 comments

How can i dispatch a redux action from code which is outside of react class?

teosz avatar Jan 14 '16 13:01 teosz

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.

timjacobi avatar Jan 15 '16 10:01 timjacobi

This. Also you can also grab dispatch as a prop from your connected components and pass it anywhere you like.

gaearon avatar Jan 15 '16 20:01 gaearon

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 ?

victorredondoGL avatar Jun 02 '17 15:06 victorredondoGL

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.

timdorr avatar Jun 02 '17 16:06 timdorr