New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling store.dispatch() from a React component file? #916
Comments
Check out https://github.com/rackt/react-redux for a helper method to bind dispatch to your components |
Specifically, from https://redux.js.org/docs/basics/UsageWithReact.html
|
This won't work if you render your app on server because you want to have different Why not do this instead? import { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions';
let createHandlers = function(dispatch) {
let onClick = function(node, data) {
dispatch(actions.nodeClicked(data))
};
return {
onClick,
// other handlers
};
}
class Controls extends Component {
constructor(props) {
super(props);
this.handlers = createHandlers(this.props.dispatch);
}
render() {
// pass this.handlers anywhere you'd like
}
}
export default connect()(Controls); |
@gaearon Looks great, thank you! |
@gaearon Actually, after trying it out, I'm getting The relevant parts of my code:
However, after reading another issue (#239) I realized that I am actually binding my actions to
|
Because you are not running the code I posted. In the code I posted Both approaches are equivalent though so go with what reads best to you. |
👍 Ok, that makes sense now. Thanks for the help! |
Kinda late to the party, but replying just in case it helps someone later (including myself).
|
Hi. I also have this problem.
How can I use |
@EhsanFahami : This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux where there are a lot more people ready to help you out - you'll probably get a better answer faster. Thanks! |
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 thestore
instance?Right now I have the following, which works, but I'm not sure that it's the right approach:
index.jsx:
Controls.jsx
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.The text was updated successfully, but these errors were encountered: