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
Can't get store from context #193
Comments
I actually have the same problem. I do use react router.
I also noticed the exact same thing that
|
Having same issue, both with and without React Router. ps: I've ensured that there is only one React instance loaded
|
You are not supposed to access this.context.store anyway. You use connect to pick the part of the state you want. React only populates the context if you specify contextTypes in your component. You will however notice that the props in your component is populated from the store, as you specified in your mapStateToProps function. If your want to subscribe to state changes, make sure you set contextTypes. Or, even better, handle the subscriptions on the root level (where you create the store). |
Hi @awestroke; So, Do You confirm that the store object is not injected into props? About the subscribing, isn't store.subscribe purpose just to be used to listen and retrieve a piace of state from store? |
@awestroke, I am using connect to pick the parts of the state that I want. But that doesn't let me subscribe to state changes. I realise react will re render when the connected parts change, but I think it is a perfectly valid use case to observe changes to a specific property on the store, and trigger an action based on this - not all state changes need to trigger a re render. If the observed property can be updated via various means, this is a neat way of responding to that. Yes I could do this where I setup my store, but then I end up with irrelevant logic together in that file. I'm using redux-rx's observableFromStore to do this, which requires the store passed in. I'm pretty sure I've already tried to set contextTypes without success, but can try again tomorrow. |
@awestroke was correct, I just needed to correctly specify
|
@awestroke @marcuswhit So it is confirmed that store is not passed in with props? |
There are two possible approaches here. Use
|
Thanks @gaearon I get that you should only use
Essentially, I watch for any changes to the ccyPair on a tile object, and then subscribe to the price stream each time. The ccyPair could be changed by the user or programatically in a reducer, so putting this logic in a component handler doesn't quite fit. I'm defining this as a payload on an action, which then gets handled by Is this a valid use case for observing the store? If I just use |
@marcuswhit Can you show your whole component? I'm confused why it needs both |
Sure. I think I need both the
.
|
Any thoughts on the above @gaearon? |
Seems OK to me. Yes, getting store from context explicitly seems like best option here. |
@gaearon Is there any better way to use store.subscribe other than getting store from context ('connect' doesn't help)? I'm new to Redux but I feel it's quite weird when I try to follow http://redux.js.org/docs/api/Store.html doc to do subscribe but don't know how to get the store object. Searched a long time on the internet and finally get here. I feel there should be a more apparent place to let people know the normal way to get store for store.subscribe(). |
@shlinx : Dan isn't actively maintaining Redux any more. As Dan said earlier in the thread, 99.9% of the time, you shouldn't be trying to access the store directly in your React component. Use the There's also many other Redux tutorials listed at https://github.com/markerikson/react-redux-links/blob/master/redux-tutorials.md as well. |
@markerikson Thanks. |
@markerikson What if I want to pass state but don't want to subscribe to store updates? Whenever I pass |
@Damnum |
@Damnum: could you share a scenario where you do not want to subscribe to state changes? |
Sorry guys, after some investigation I realized that my question was based on a misunderstanding how/when components are rendered in React. So I solved it differently now. Thanks anyway! |
How do you import |
Use the prop-types package. import PropTypes from 'prop-types' |
My top level component is like so:
And my App component is like so:
I'm not using React Router, and I've ensured that there is only one React instance loaded.
If I break at the top of
App.render
,this.context
has no store on it. However,this._reactInternalInstance._context
does have the store attached. Obviously I can grab this, but I'm guessing I shouldn't really use that one.Why is there no store on
this.context
? I want to subscribe to certain state changes.Thanks.
The text was updated successfully, but these errors were encountered: