React-redux: Expose store.dispatch?

Created on 14 Jan 2016  路  4Comments  路  Source: reduxjs/react-redux

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

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nainardev picture nainardev  路  3Comments

a-koka picture a-koka  路  3Comments

IbraheemAlSaady picture IbraheemAlSaady  路  3Comments

nmaves picture nmaves  路  3Comments

winterbe picture winterbe  路  3Comments