-
-
Notifications
You must be signed in to change notification settings - Fork 10.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
Make a higher-order component to get the router from context: #3350
Comments
Adding it now... |
Yes please |
While we're on the topic, this is my new fav for context: https://gist.github.com/ryanflorence/1e1290571337ebcea1c5a748e8f5b37d |
@timdorr awesome :) |
What is the migration strategy for those currently using context? Will there be depreciation warnings with guidance? |
@Blesh Keep using it. This is just a convenience. |
This would be an addition—not breaking existing code. However I’d encourage to use it in case React changes the exact context API. |
@Blesh We can't deprecate React's context API, and we'll continue to provide the same object on I'd probably suggest using |
Sounds oddly familiar |
Was thinking of you :P At the time I don't think what we provided on context was as clean as it is now. |
I also like Would it be possible to make this component in such a way that it can be used as a decorator (but doesn't have to be)? I really like how that works with @connect(myApp.todos.connector)
@withRouter
class Todos extends React.Component {
clickHandler() {
this.props.router.push('/yeah');
}
// ...
} The name |
Yeah, it's problematic that it's not opt-in when it comes to wanting to deprecate usage. @ryanflorence couldn't you return a proxied getChildContext() {
var proxiedRouter = {
actual: router,
push() {
console.warn('using router from context is deprecated, please use `withRouter` (docs link here)');
this.actual.push.apply(this.actual, arguments);
}
};
} (Sorry, I'm not familiar with the code-base, it's just a suggestion). |
I'd prefer we keep We just don't want to force all users to use |
Multiple ways to do the same thing might be problematic on larger codebases and bigger teams. |
should this be included in the router when it is available here? const withRouter = getContext({ router: React.PropTypes.object }) |
This is my thinking too. But I'm in the camp of keeping |
@nfcampos that's a great question. I have no way of knowing our users as compared to react at large, but we have 1/2-2/3 the downloads of react from npm. I think that means a lot of folks are coming to React and Router together, and it would be nice to not send them on dependency goose-chase to use our API. "Before you can navigate around in your component, you need to install Is a lot to ask a new user. |
I think that's on you (or your architects, rather). As it stands, we already support both doing e.g. I'm very much 👍 on not forcing users to use a feature documented as "experimental" just to be able to call |
I think all of our docs should use |
Hmm, this is true. It's not like we're wrapping up I just don't want to do all that rewriting... 😫 |
@ryanflorence Good point, probably worth it to add it in then. |
I agree with @timdorr. I think introducing First of all, there is a reason 'we' (i you'll permit me :) have The fact that stateless function components are the recommended way to write React components now and have a Currently, React Router places a router object on context that we can use. Almost as easily as By making a
If you want to avoid the side effect, you can make a HoC that has Either way, please make sure the child component can just use Furthermore, choosing for My reasoning is that, given React's user base, EDIT: Still, it begs the question if it's important enough to |
The idea here is just to do what Redux, React Intl, and most other libraries do. If you use You may not feel that
|
@taion I get it and I even sorta like it. From the perspective of the author of that component, it's great! The problem comes when we mix and match components from different authors. Some like I just have this feeling coming up that when I copy and paste some code, I'm gonna keep having to search-replace Personally I think either would be fine, but having both is sorta... Yuck. And since it's on Re the 'experimental' quote. Mmm I'm not sure what to think of that. Having the signature for the recommended way to writing components be function MyComponent(props, context) {
// render here using props and.... context!
} ... and then acting like the API could change at any time is quite at odds with each other in my book. And in situations like these I tend to go with the code and not the docs. Whether Facebook likes it or not, changing |
Nope. There are tons of use-cases for component state, you might be reading too many blog posts :P
Our docs will say We've been traveling down the context-as-our-public-api-not-just-implementation road you're talking about, we have bruises, aches, and pains, and we might need our left leg removed. We look over at redux and react-intl and they don't have any bruises, but still get to use context for their libs. Had we used this API in the first place:
|
React API documentation, actually:
https://facebook.github.io/react/docs/reusable-components.html#stateless-functions |
I'll also add that I can't think of any React Router extensions that expose their use of the router context object, either via context directly or via the wrapper. Most of them fully encapsulate that use, so you won't even see this inconsistency through pulling in third-party code. |
@ryanflorence ... regarding me previous comments. I was thinking about it last night, and someone could probably make a Babel plugin that warns about So, if you were worried/thinking about what I had said (I don't think you were, but just in case), disregard. |
We can't.
Context is React's API, not ours. Ours is the thing we put on context, however that gets accessed in React isn't our problem, our problem is simply the shape and function of that object.
This may or may not happen. But context right now has the potential for naming collisions, which would go away if a component was only allowed to provide one context type, rather than multiple contextTypes. Similarly, components would only be able to ask for a single contextType, thus preventing naming collisions (but still allowing shadowing). I don't know if anybody is actively working on it. |
@insin was it mooted? where? |
Sorry I did not mean to confuse like I did :) I get that you don't want to remove support technically. Just saying that the problems you are citing with EDIT:
Currently, Taion is suggesting that
Not according to this project's API docs. EDIT 2: |
Just my 2 cents: in my own project, I already use a thing called |
|
I'm 👍 too.
Yup, you are right. But 'deprecating'
I'm not too sure about that because if Facebook would, say, rename I'm hoping you agree that, no, that wouldn't be cool. unless we would remove all mention of It sounds to me that you guys are basically saying you are only adding |
I just realize that the API docs in it's current form don't even mention that Facebook considers |
@ryanflorence From The Notghost of Reactmas Yet to Come: https://twitter.com/sebmarkbage/status/669313585940553728
https://twitter.com/sebmarkbage/status/717384903763959808
|
This discussion is going way, way off track. Here's the plan:
As our underlying implementation relies on There's no difference – this is just adding an API for users who want to avoid potential instability with We're going to try to land this in the next minor release. |
Ok you're going to cheat :) 👍 |
No part of the API is being removed. |
Yeah, especially after having read those tweets (thanks @insin ) I think I will prefer it as well. Since it is not getting removed, that means breaking changes to |
Any breaking changes to The only difference is that if you're using |
... which in semver is exactly the difference between a major and a minor bump. You could do a minor bump if you would remove @taion Sorry for being pedantic but if you read carefully you'll see that you and Ryan are not saying the same thing; Ryan is implying I'm in your camp :) If React is bumping the major, why would it be strange that React Router follows suit? Keep it around and wait to see what happens with that experimental status before deciding here. Still, a warning in the docs about that experimental status might be good. |
@Download I see many places in this discussion where Ryan says that |
I don't think you're understanding my point. If React completely changes how I'd prefer not to do this, and potentially it might not be strictly needed, but we probably want to treat (nontrivial) breaking changes to peer dependencies as breaking changes anyway. |
Great point. I hadn't thought about that. @vcarl
Somehow, making |
It does not alleviate the need for major version bumps. What it means is that if you use But we'd still need to bump the major version, &c. |
Released on v2.4.0. |
how do you get the current location when using withRouter() ? this.props.router is my router object, but this.props.router.location is undefined same ngoes for this.props.router.pathname and this.props.router.currentPathname |
You can access current location via this.props.location |
this.props.location is also undefined when using withRouter() |
@jonaswindey @lastnamelava It's available on 3.0.0-beta.1: https://github.com/ReactTraining/react-router/blob/v3/modules/withRouter.js#L33 |
The documentation is not clear at all on how |
When React documented context we were like "YAY! we can just tell everybody to use context for the router." But we were probably over-confident in its stability and people's comfort with that API. I think we can wrap up everything into a higher-order component like:
context.router
#3346)We could probably even provide a
this.props.confirmOnLeaveRoute
shortcut like theLifecycle
mixin used to provide.Whose with me?
(yes, this has been talked about before, thanks to those who tried to get this in before :)
Potential names:
connectRouter
like Redux. Seems weird to me, not sure if that name is about connecting to the store, or to the data in the store.injectRouter
like React Intl. I'm also iffy on this since it's not technically "injection", you're asking for it, not injecting it.withRouter
but whatever ... is there a community name for this yet? If not, can we start lobbying for this one?The text was updated successfully, but these errors were encountered: