-
-
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
[wip][added] Routing middlewares #2376
Conversation
TODO: Add docs if we like this feature
- Show more typical use of middleware factories - Test that middlewares receive expected props
@ryanflorence If this PR is accepted, I would like to drop |
I think this should be held to post-1.0. We keep breaking APIs on a release candidate, so we're never going to actually release something. I know there are folks waiting on a 1.0 stable. |
This isn't a breaking API change, though - this is purely additive, so it would be a minor version bump per semver. The reason I care is that this or something like it that allows access to the array of components is a pre-requisite for relay-tools/react-router-relay#12. |
In general I think asking people to provide a |
Thinking about this a bit more, the middlewares array is not ideal. Maybe a better API could be: <Router history={history}>
<Middleware1>
<Middleware2>
{routes}
</Middleware2>
</Middleware1>
</Router> With <RoutingContext {...renderProps}>
<Middleware1>
<Middleware2 />
</Middleware1>
</RoutingContext> I don't think I can get all of what I want with a history enhancer. The issue for
I believe both It might be possible to get something like (1) via a history enhancer, but I think getting (2) requires something like this (for modifying |
The other motivation here is to move away from the Something like a composable middleware pattern for modifying element rendering would address that concern and offer a better entry point for libraries. |
Correction - the server-side example should perhaps be: <Middleware1 {...renderProps}>
<Middleware2>
<RoutingContext />
</Middleware2>
</Middleware1> |
This approach is dumb. I'm dropping this PR and taking a different approach. |
I think the core problem is trying represent a functional concept like middleware with JSX. There are some similarities in terms of nesting, but significant differences in how you might code something like that. I think what you're looking for is something closer to redux's version of middleware, which is just an enhancer. So you would end up with something like this: import { Router, applyMiddleware } from 'react-router'
import someMiddleware from 'react-really-cool-shit'
import someOtherMiddleware from 'react-quite-neat-thing'
const RouterPlus = applyMiddleware([
someMiddleware,
someOtherMiddleware({ option: 123 })
], Router)
React.render(
<RouterPlus>
<Route path="/" component={App} />
</RouterPlus>,
rootDOM
) |
The issue in some sense, and the difficulty around all of this is that I'm trying to apply middleware to the Short of doing something really nasty with ES6 inheritance, I can't meaningfully get what I need by modifying the I'm going to submit another, simpler PR shortly. |
I built this a bit ago, I really dig it, haven't had a chance for show and tell to get people's feedback on it though: https://github.com/ryanflorence/react-router-apply-middleware |
@ryanflorence That's pretty cool. Why don't we have that in core? 😄 |
I really like the API – it's much cleaner than the fiddly stuff I have in e.g. react-router-relay, and avoids an issue with route handler containers getting applied in the opposite order. Maybe we should discuss this on a new issue instead of a closed, half-year-old PR though 😆 |
Or even better, a PR to add it 👍 |
TODO: Add docs if we like this feature
Closes #2088
I decided to get this out there for visibility before spending time adding explicit docs. The new test case shows this in action, including a bit of an unavoidable quirk.
There's a few things behind this idea:
createElement
is not ideal as a point for general customizability because it's not composable - a user doesn't get anything to allow e.g. using bothReactRouterRelay.createElement
and some other customcreateElement
functioncreateElement
, but it's fairly annoying to do so: https://github.com/relay-tools/react-router-relay/blob/v0.7.0/src/Container.js#L20-L23 (i.e. expose something on child context, and check whether that thing is there - if not, you must be on the root)components
androutes
), while not forcing us to expose those as props to all route components, which shouldn't need access to all of those (i.e. we could revert the commit fixing "branch" (or "routes") in beta4 #1788 and removeroutes
from the props injected to route components again)Let me know what y'all think.