-
-
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
[added] <Routes handlerProps = {…} > #374
Conversation
Anything you pass into handlerProps will be passed as props into all your handlers. For instance: <Routes handlerProps = { { "favoriteFruit": "banana", "favoriteDrink": "smoothie" } } > <Route handler = { Trunk } > <Route handler = { Branch } > <Route handler = { Leaf } /> </Route> </Route> </Routes> if Leaf was the active route; Trunk, Branch, and Leaf would all be called with favoriteFruit and favoriteDrink as props, like so: <Trunk favoriteFruit = "banana" favoriteDrink = "smoothie" > <Branch favoriteFruit = "banana" favoriteDrink = "smoothie" > <Leaf favoriteFruit = "banana" favoriteDrink = "smoothie" /> </Branch> </Trunk>
@appsforartists Thanks for this PR! It looks great. I'm not sure we want to merge it just yet, because the whole |
I definitely understand wanting to keep API creep to a minimum. FWIW, I haven't seen any proposals in those threads that address the I'd really like to avoid manually passing props to a specific route (at least for the use cases here and in #314). It makes it way easier to build an ecosystem around ReactRouter if there's a single point-of-contact that can marshall data into the handlers. Do you see a way to handle both the global and local use cases with the same solution? |
If there is the need to support both global-and-local props, one implementation would be having a In Angular's |
@appsforartists Thanks for explaining how |
Sure thing. I once worked on an admin console that was built in Angular. The routes worked something like this:
|
Because of the way Angular works, those were blocking calls. You could return a promise instead of a value, and the transition would be blocked until the promise resolved. |
This commit adds the ability for route handlers to load props they need for a given route, optionally asynchronously. Props loaded in this manner cascade from route handlers further up the hierarchy to children, such that props loaded by children take precedence. getRouteProps should return a "hash" of props, the values of which may either be immediate or deferred using promises. As promises resolve, forceUpdate is used to re-render the <Routes>. If no props are promises, the operation is fully synchronous and route handlers will have all the props they need on the initial render. As implemented, this work should satisfy the use cases in #319, #261, #314, #374, and (indirectly) #262.
I tried implementing something similar with @rpflorence + @mjackson, it sounds like you're leaning towards closing this in favor of #396. This may sound crazy but considering the tiny footprint of this PR, would you be amenable to making #396 the recommended/documented way to pass props into handlers, but landing #374 for advanced uses (e.g. writing libraries that build off of RR)? As soon I have permission to do so, I intend to open-souce my library for the good of the greater React community, so this is about more than some crazy guy doing a demo. (If I wasn't so concerned about keeping the concerns separate so I can open-source this later, I'd have found a solve that didn't rely on #374.) I know it's crazy, but please let me know if it's something worth entertaining. |
I'll even rename it |
@appsforartists You don't need If you need a "global" settings object like the one you're describing, you can just use plain JS to solve your use case. Check this out: With
|
Anything you pass into handlerProps will be passed as props into all your handlers.
For instance:
if Leaf was the active route; Trunk, Branch, and Leaf would all be
called with favoriteFruit and favoriteDrink as props, like so:
If the test can be improved, please let me know!