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
How can we access redux in componentDidMount with react-router 1.0? #239
Comments
The instance of class Foo extends React.Component {
static contextTypes = {
redux: React.PropTypes.object
}
componentDidMount () {
this.context.redux.getState()
}
} You might want to look at some of the examples. |
Thanks for the quick answer! I think I saw this in one of the many examples about redux but since I didn't found it the the redux documentation I felt that this was more a hack that the actual good way to go. Just to make sure I understand correctly, is it the Provider that make the redux instance accessible through react context? |
Correct, the instance of |
Thanks! |
Is this applicable in v1.0.0-rc? I got undefined in context.redux. :( |
Indeed, you're going a slightly wrong way. Alternative 1: Use
|
@gaearon thanks a lot for your insights! Regarding the alternative 1I actually started using the For example: import HomePage from './home-page.jsx';
import * as HomeActions from './home-actions.js';
import { bindActionCreators } from 'redux';
import { Connector } from 'redux/react';
function select(state) {
return { list: state.home.list[this.props.params.listID] };
}
// ...
render() {
return (
<Connector select={select.bind(this)}>
{ ({ list, dispatch }) => <HomePage list={ list } /> }
</Connector>
);
}
} Is it possible to achieve similar extraction of props using the And I see that you're using Regarding the alternative 2I am using a promise middleware and I imagine that I could call the thunk middleware before it to achieve what you suggest. I really like the idea of avoiding the test in Again, thanks for the time you took to explain and thanks for redux! :) |
With |
Thanks @gaearon. I chose not to use
or maybe you have a more beautiful approach? |
Just noting that you don't have to use this.context.store.dispatch(actions.login(credentials)) |
Ahh I see.. Thank you very much.. :) |
Thanks, I didn't knew about the |
@happypoulp That's what |
Thanks a lot! |
Also But as @gaearon mentioned, the new upcoming |
Hi,
I feel that this question is maybe half a redux question for the best practices of how to use redux and half a react-router question for the technical part regarding how to pass props to child components.
I use redux 0.12 and react-router 1.0 beta.
I am having a hard time figuring how to access redux and to read in its store from componentDidMount.
Here is an extract of the code I use:
./routes.jsx
./client.js
./app.jsx
./home-page-container.js
With react-router 0.13, I could pass redux as a prop to the Handler chosen by the router but with react-router 1.0 there is no such thing as a Handler component to pass props to.
Example
With react-router 0.13 I could do:
and if Handler == HomePageContainer I could access this.props.redux in HomePageContainer::componentDidMount
But with react-router 1.0 we now have
and if App::this.props.children == HomePageContainer I don't know how to access redux in HomePageContainer::componentDidMount.
I didn't even figured out how the association Router / Provider is working (I guess I lack some understanding of react router 1.0 itself here).
So here are my questions:
Sorry for the many questions and thanks for any help!
The text was updated successfully, but these errors were encountered: