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 react component context in mapStateToProps()

See original GitHub issue

Hi,

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

github_iconTop GitHub Comments

16reactions
adriancooneycommented, Mar 26, 2016

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 are connect’d so if they’re linked directly, they know what state to load. For example:

<Route path=":username/:repo" component={Repo}>
    <Route path="issues" component={Issues} />
</Route>

The Repo connected component loads the data it needs to display and selects it from the state in it’s mapStateToProps. Say, for instance, it selects the repoOwner and repoInfo from the state. The child component Issues loads the data it needs to display and selects it from the state. It also needs the repoOwner and repoInfo state too so instead of performing the selection again (which is quite complicated), it’s passed down via context. The problem arises when the Issues component needs information from repoOwner in it’s mapStateToProps, 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:

class IssuesWrapper extends Component {
    static contextTypes = {
        repoOwner: React.PropTypes.object
    }

    render() {
        return <Issues repoOwner={this.context.repoOwner} />
    }
}

<Route path=":username/:repo" component={Repo}>
    <Route path="issues" component={IssuesWrapper} />
</Route>

I feel as though a third argument to mapStateToProps, like React’s lifecycle methods, with the context could be pretty useful so I could access repoOwner directly instead of creating the wrapper. This may be hard to implement, however, given the magic react-redux does with the function.length on the mapStateToProps function (i.e. if you want context, you have to suffer the performance impact from including props argument) so I understand it’s a tough decision.

5reactions
jimbollacommented, Jan 18, 2017

Until the day comes that React has a fully blessed context API, adding support for it is a maintenance liability for React Redux.

Read more comments on GitHub >

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

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