-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Export context to allow history access #6365
Comments
I think you may be confusing 2 ideas. I'm proposing that we stop rendering something with a <Route path="*" children={({ history }) => (
<button onClick={() => history.push('/users')}>Go</button>
</Route>
// or even
<Route children={({ history }) => (
<button onClick={() => history.push('/users')}>Go</button>
</Route> In fact, we use a |
BTW I'm a pretty strong -1 about exposing context as public API. That's our internal API. Our public API is |
BTW, if it helps, I'm getting the strong sense that |
@timdorr I use many context in my app. import { t } from '../locale.js';
// localeContext.unstable_read().t under the hook
export const Component = () => (
t('user')
) |
@timdorr |
@mjackson AFAIK exporting context is going to be normal/expected with what's coming. Formik is going to do the same. |
Just a reminder: You will always be able to get to it via a direct import ( |
@jaredpalmer It just seems like context shouldn't be public. It's just a communication channel that we use to communicate between our components since we can't pass props down at every level. |
I think the goal should be to eventually drop Edit: To elaborate, I think of a context consumer (read from) as being public, while a provider (write to) is private. A Exposing the consumer will also let users use whatever future context optimizations for class components there are ( |
While I agree with @mjackson that we shouldn't access the private context, I have a argument to why context would be better. As we know The downside to withRouter and the HOC is that the react dev tools show a bunch of routes everywhere. Getting access to the context Am I totally off here or is there a secret way to gain access to router props without wrapping your component in a route. |
The fact that we use context under the hood is just an implementation detail! Take the 4.4 release, for example. If context were part of our public API, changing from the old context to the new one would have been a breaking change, and we would've had a major version bump. But since it's not, we are free to refactor and move things around while still providing the same high-level API. That's the beautiful part about us not exposing context. Also, what if the context implementation changes again (and it could)? If we expose it as public API, we will have the same problem. |
Just to address the examples in the OP: For the first example, const Component = () => (
<Route>
{({ history }) => (
<button onClick={() => {
if (history) {
history.push('/users')
}
}}>Open users</button>
)}
</Route>
) For the second example and in preparation of facebook/react#13728 landing, we could set a class Component extends React.Component {
handler = () => {
this.props.history.push('/users')
}
}
export default withRouter(Component) And for the last example, I'm not sure if The React team definitely wants to discourage context |
I'd say we can go ahead and close this for now since neither I nor @timdorr see any reason why you should need direct access to context. Happy to reopen if this changes in the future. |
Ref #6362 (comment)
Since "always render" behaviour will be removed from
<Route children />
it would be good to provide a way to access context values from any part of app.Here's a few ways and all requires to provide
RouterContext
.RouterContext.Consumer
Class component contextType
context.unstable_read api
The text was updated successfully, but these errors were encountered: