Skip to content

Calling store.dispatch() from a React component file? #916

@alexklibisz

Description

@alexklibisz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions