Access react component context in mapStateToProps()
See original GitHub issueHi,
I know that binding the component context to props as been discussed and is not relevant yet.
What about reading the component own context. I need something like:
connect((appState, ownProps, ownContext) => { ... })
Is that possible?
Using react-router, I need to access some url params through inner components. I want to pass them through contextType. Then those url params are used in mapStateToProps() function to target the right stores on appState.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:13 (5 by maintainers)
Top Results From Across the Web
How to use mapStateToProps to get a value from the state ...
React -Redux provide the connect api that accept a mapStateToProps function with state and props as a component. What I would like, if...
Read more >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 >State Management with React Hooks and Context API
And then all those connect , mapStateToProps and other functions to be able to access the state? Many React developers default to Redux...
Read more >React Redux connect(): When and how to use it
React Context ; How does Redux connect() work? Using mapStateToProps in React ... You can access these props in the component as follows:...
Read more >Redux Minus Redux With React Contexts - Carbon Five Blog
jsx ... export const connect = ( mapStateToProps = () => ({}), mapDispatchToProps = (dispatch) => ({dispatch}) ) => (Component) ...
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
You seem pretty set on your decision, Dan, but I thought I’d give you some perspective my situation where this feature might be useful for consideration.
Each of my
<Route />
components areconnect
’d so if they’re linked directly, they know what state to load. For example:The
Repo
connected component loads the data it needs to display and selects it from the state in it’smapStateToProps
. Say, for instance, it selects therepoOwner
andrepoInfo
from the state. The child componentIssues
loads the data it needs to display and selects it from the state. It also needs therepoOwner
andrepoInfo
state too so instead of performing the selection again (which is quite complicated), it’s passed down via context. The problem arises when theIssues
component needs information fromrepoOwner
in it’smapStateToProps
, which isn’t available because it’s in the context.My hacky solution is to just create a wrapper component that passes the context as a prop but it feels dirty and I end up with a lot of useless components filling up the tree:
I feel as though a third argument to
mapStateToProps
, like React’s lifecycle methods, with thecontext
could be pretty useful so I could accessrepoOwner
directly instead of creating the wrapper. This may be hard to implement, however, given the magicreact-redux
does with thefunction.length
on themapStateToProps
function (i.e. if you want context, you have to suffer the performance impact from includingprops
argument) so I understand it’s a tough decision.Until the day comes that React has a fully blessed context API, adding support for it is a maintenance liability for React Redux.