-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Access store from context? #1123
Comments
We use the new context API, which is exported as |
Got it working but this also seem to subscribe to updates from the store. I can of course export it from where I created it. Is that the best way? |
What are you actually trying to accomplish? |
I want to read a value from the store in componentWillUnmount. Previously I got access with this.context.store.getState() |
Per the React docs on using context, you would either need to add a What kind of value are you needing to read from the store on unmount? Why not just connect the component, and extract that value in |
I'm just trying to think as performant as possible. Hence not wanting to subscribe if I don't need all the updates. |
As I asked, what kind of value are you trying to use? Is it something that you expect will be changing frequently? Do you have specific reasons why you think connecting to get this value will be a performance issue? Why do you only need this in My general reaction here is that you're over-thinking the performance implications, but I can't say for sure because I don't know the specifics of your app and what you're trying to do. |
Ok. I used contextType before upgrading to 6.0.0. When I upgraded it stopped working... The release notes said this: The value I want to read is a string that does change quite a lot and in this component I only need it when it unmounts. It's used for statistics tracking in this case. |
Note that 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 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
}
} |
Ahh, didn't know about that way :) Will try it first thing tomorrow. |
contextType seem to work the same way as Context.Consumer in this case. meaning I get updates. |
No, the React-Redux |
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 👍 |
You can use ReactReduxContext from 'react-redux' : |
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
How do i achieve the same thing with 6.0.0?
The text was updated successfully, but these errors were encountered: