question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get store from context

See original GitHub issue

My 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:closed
  • Created 8 years ago
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

38reactions
gaearoncommented, Nov 24, 2015

There are two possible approaches here.

Use connect() and don’t worry about the context

This 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 a connect()ed component is an anti-pattern anyway. Let connect() take care of subscriptions. If you want to cause a side effect when store state changes, do this in componentWillReceiveProps of the connect()ed component.

Specify contextTypes for store and access context directly

If you really want to grab store from context yourself (my advice: don’t do this unless you have a very good reason!) you need to specify contextTypes. Context is opt-in in React, and you won’t receive this.context if you don’t specify contextTypes. My free Egghead lesson on implementing <Provider> with context covers that.

13reactions
marcuswhitcommented, Nov 23, 2015

@awestroke was correct, I just needed to correctly specify contextTypes in my component, and the store was then added as expected:

App.contextTypes = { store: React.PropTypes.object };
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found