-
-
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
Provide component middleware helper and documentation #3316
Comments
If I can get a +1 from somebody else I'll work up a PR |
Seems like providing both render and createElement is overkill. Can't we
|
You need both, the root container provides some context that the container passes to route components. For example, in AsyncProps, the root container keeps the data in state, puts it on context, and then the Container pulls it from context and passes it as props to the route component. Relay router also needs both. The router renders in two places, the top level |
I'm 👍 on this change, specifically with the two render hooks. I think the If you take a look at what I have in react-router-relay, it's a bit fiddly. I need to both set the default value for There's also another problem with this approach of wrapping <FooRootContainer>
<BarRootContainer>
<RouterContext />
</BarRootContainer>
</FooRootContainer> Because of the way the wrapped <BarRouteContainer>
<FooRouteContainer>
<RouteComponent />
</FooRouteContainer>
</BarRouteContainer> This might not be a problem in practice, but it's a bit confusing and awkward. |
To implement relative routes, I only needed Again, the router takes control of rendering in two places, so middleware needs to be able to participate in both places. |
Any reason we don't do this already? We had talked about doing this in the past to build up support for relative Links being a core feature at some point. |
I like trying to enhance the router from the outside to make sure we have a decent API for extending. We'll bring it into core though when it's working well enough. |
follow #3327 |
Is this usable in an isomorphic app? |
The result of const RouterContext = applyRouterMiddleware(middlewares)
// Then that's your <RouterContext> to render with! |
@taion, I see, thanks! |
I cooked this up a little bit ago and I really like it:
https://github.com/ryanflorence/react-router-apply-middleware
Router middleware can be really powerful, but it's tricky to build it in a way that makes it composable with other middleware because you have to compose both the
render
prop and thecreateElement
prop, for example, to compose just three middleware together it would look like this:All the
renders
go first, then all thecreateElements
go into the lastrender
in the chain, and each has to work as the first, last, or middle in the chain. And so you have to hope that the authors of all three did everything right w/ their ownrender
andcreateElement
props. They'd all have to do the exact same thing.This API looks a little crazy when used like this, but I don't think that means the router has a bad extension API--I think it's solid--but we can definitely clean up the front-door to it.
applyRouterMiddleware
makes middleware composable by default and much simpler for both the authors and consumers:Consumers have way less to do now:
It's also pretty easy for middleware authors. A middleware is just a function that provides an object with two render methods, one for
render
and one forcreateElement
.Then
applyMiddleware
takes all of those elements and creates that mess from earlier. Authors no longer have to providerender
andcreateElement
props or even think about how to compose w/ others, they just have to worry about what their components do.I'm undecided on the method names. I picked
renderRootContainer
andrenderContainer
because when I author middleware I usually use aRootContainer
inrender
and aContainer
increateElement
, maybe they should berender
andcreateElement
so they map exactly to the Router's names for these methods.I'd like to bring this into core and then document how to author middleware so folks can add whatever middleware they need and have it compose with everybody else's.
This is a follow up from #2376
The text was updated successfully, but these errors were encountered: