-
-
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 react component context in mapStateToProps() #289
Comments
We don’t want to expose If you’d like to read something from context, please read it in a parent component, and pass it to the |
Yes It is actually the solution I implemented . Thank you for the advices. I didn't read anything about what will become this context. Could you give me a link so I could stay up to date with React future changes? Thank you! |
There’s no specific link (it was a discussion on Twitter) but AFAIK the plan it to allow a component to receive only a single kind of context. So instead of |
Hi @gaearon Since React-Router allows you get access to the router, history, location etc.... via the context, how can you get access to the context from mapStateToProps() ? Since its now a documented feature of React, react-redux should expose context in the public api, right ? Best regards ? |
I believe I answered this in #289 (comment). We also can’t possibly read arbitrary context because we’d need to know your contextTypes. Please pass this info as props from parent. |
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 path=":username/:repo" component={Repo}>
<Route path="issues" component={Issues} />
</Route> The 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 |
I've made a "Provider" component to expose my Axios instance it in a context, allowing container components to inject them in my thunk dispatchers, but I think I'm doing to much repeated work, something that I've done similar things using props, and it looked pretty good: // Action creator
export const fetchSorting = (sorting) => (param1, param2) => (dispatch, getState) => { ... } // Component
class SortedList extends React.Component { ... }
const mapDispatchToProps = (dispatch, ownProps) => ({
doFetch: bindActionCreators(fetchSorting(ownProps.sorting), dispatch),
});
export default connect(undefined, mapDispatchToProps)(SortedList); The authors say, the usage of context is discouraged, but passing down the tree as a prop is quite painful, and I repeating the steps of react-redux and creating my own |
Can you at least make it available to function selectorFactory(dispatch, factoryOptions) {
return (nextState, nextProps, nextContext) => {
return Object.assign({}, nextProps, mapStateToProps(nextState, nextProps, nextContect), mapDispatchToProps(dispatch, nextProps, nextContect));
};
} |
See #599 (comment) and #599 (comment) |
@jimbolla By only injecting it into Whether or not the context is passed to the inner function of
I did manage to get |
Until the day comes that React has a fully blessed context API, adding support for it is a maintenance liability for React Redux. |
@adriancooney what solution have you implemented in the end? My dirty way:
and in the component:
|
@zergione in the end I opted to manage the state globally. I took a bit more effort but it felt more organised within the app and not tied to the implementation. |
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.
The text was updated successfully, but these errors were encountered: