Can't get store from context
See original GitHub issueMy top level component is like so:
ReactDOM.render(
<div>
<Provider store={store}>
<App />
</Provider>
</div>,
document.getElementById('app')
);
And my App component is like so:
@connect(state => state)
export default class App extends React.Component {
render() {
return (
<div className={cx('container')}>
<Workspace />
<Blotter />
</div>
);
}
}
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.
Issue Analytics
- State:
- Created 8 years ago
- Comments:22 (5 by maintainers)
Top Results From Across the Web
Why do i get "Could not find "store" in the context of "Connect ...
I have a component which I'm trying to connect with the global redux store. But when I do, ...
Read more >Accessing the Store | React Redux
In rare cases, you may need to access the Redux store directly in your own components. This can be done by rendering the...
Read more >Could not find - "store" - in - either the context or props
It's saying your connected component cannot be rendered without a store. So, if your current call to shallow is like this: const wrapper...
Read more >How to Work with the React Context API - Toptal
Let's refactor the app and demonstrate what it can do. In a few words, the Context API allows you to have a central...
Read more >How to use React Context effectively - Kent C. Dodds
If it's just using the default value that's been provided, then it can't really do much good. 99% of the time that you're...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There are two possible approaches here.
Use
connect()
and don’t worry about the contextThis is the approach we suggest. Use
connect()
, let it subscribe to the store, and don’t worry about the React context API. There are limitations: indeed,store
itself is not passed down as a prop, but subscribing to it inside aconnect()
ed component is an anti-pattern anyway. Letconnect()
take care of subscriptions. If you want to cause a side effect when store state changes, do this incomponentWillReceiveProps
of theconnect()
ed component.Specify
contextTypes
forstore
and accesscontext
directlyIf you really want to grab
store
fromcontext
yourself (my advice: don’t do this unless you have a very good reason!) you need to specifycontextTypes
. Context is opt-in in React, and you won’t receivethis.context
if you don’t specifycontextTypes
. My free Egghead lesson on implementing<Provider>
withcontext
covers that.@awestroke was correct, I just needed to correctly specify
contextTypes
in my component, and the store was then added as expected: