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.

Access store from context?

See original GitHub issue

Hi, I just updated to 6.0.0 and have a question. I can’t really figure out from the docs how to access the store from context anymore.

My app is wrapped with <Provider store={store}>

Then in any component I could access it with

MyComponent.contextTypes = {
    store: PropTypes.object.isRequired
};

->

this.context.store

How do i achieve the same thing with 6.0.0?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

23reactions
markeriksoncommented, Dec 17, 2018

Note that contextTypes and contextType are not the same thing.

React defines legacy context using syntax like:

class MyComponent extends React.Component {}

MyComponent.contextTypes = {
    a : PropTypes.string,
    b : PropTypes.object
}

When the new context API was released, it originally could only be used via a render-props API:

<MyContext.Consumer>
{ (contextValue) => {
    // do stuff with the context value here
});
</MyContext.Consumer>

To help with the migration from old context to new context, the React team added a new way to access new context in 16.6 called contextType:

class MyClass extends React.Component {
  static contextType = MyContext;
  componentDidMount() {
    let value = this.context;
    /* perform a side-effect at mount using the value of MyContext */
  }
}

So, you should be able to do:

class MyComponent extends React.Component {
    static contextType = ReactReduxContext;

    componentWillUnmount() {
        const storeState = this.context.store.getState();
        // do something with the store state here
    }
}
7reactions
timdorrcommented, Dec 18, 2018

If you want that, you can just make your own context:

// StoreContext.js
import React from 'react'

export const StoreContext = React.createContext()

export default StoreContext
// app.js

import StoreContext from './StoreContext'

const store = createStore(reducer)

ReactDOM.render(
  <Provider store={store}>
    <StoreContext.Provider value={store}>
      <App />
    </StoreContext.Provider>
  </Provider>,
  root
)

The new API is really simple 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing the Store | React Redux
Internally, React Redux uses React's "context" feature to make the Redux store accessible to deeply nested connected components.
Read more >
Accessing the Store with React-Redux - Medium
Provider to put the Redux Store and the current state into context and connect uses ReactReduxContext.Consumer to read values and handle updates ...
Read more >
Redux: Passing the Store Down Implicitly via Context
Learn how to make the store object available to all components by using the advanced React feature called “context”.
Read more >
React: Accessing context from within a Redux store
I need to use values on the SocketProvider context in my store What does that mean? You want to use it in reducers,...
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 >

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 Hashnode Post

No results found