-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Closed
Labels
Description
I apologize if this is something that has already been cleared up, but a keyword search in the issues didn't turn up anything that helped.
If I want to call store.dispatch(someAction)
from a component file, what is the correct way to access the store
instance?
Right now I have the following, which works, but I'm not sure that it's the right approach:
index.jsx:
...
const store = applyMiddleware(
loggingMiddleware, // log every action
thunk // making API requests from actions
)(createStore)(reducer);
export {
store
};
...
Controls.jsx
import {store} from './index.jsx';
import * as actions from './actions';
...
let clickHandler = function(node, data) {
store.dispatch(actions.nodeClicked(data))
}
export const Controls = React.createClass({
render: function() {
// React component gets rendered, and it passes the clickHandler to a
// function that attaches it to a D3 visualization
}
});
Is there anything wrong with exporting/importing the store like this? I think I could just create the click handlers as part of the React component and call this.context.store
, but in reality there are about a dozen different event handlers that get passed to the D3 visualization, so that's not really practical in this situation.
harukaeru, dbacinski, gotoAndBliss, ibudisteanu, matthlavacka and 7 moreketabi