-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
v6 beta, cant pass props through Outlet. #7495
Comments
No, because you can pass them via URL parameters. |
Can we reconsider this ? There is certain props I do not want to pass via URL parameters |
You can pass them to the element in <Route path='/' component={<App />} />
<Route path /1' component={<Comp1 baz="blub" />} />
<Route path /2' component={<Comp2 foo="bar" />} />
</Route> |
@timdorr @MeiKatz Don't you find this a bit ugly? If I have a series of child routes for a dashboard that all have the same data requirements, wouldn't it rather convenient to just pass this in once through the To take another example, consider following routes definition: <Routes>
<Route path="/" element={<Homepage />} />
<UnauthRoute path="signup" element={<Signup />} />
<UnauthRoute path="signin" element={<Signin />} />
<Route path="users">
<Route path="/" element={<Navigate to="/" />} />
<AuthRoute path="settings" element={<Settings />} />
<Route path=":uid" element={<Profile />} />
</Route>
<Route path="courses" element={<Courses />}>
<Route path="/" element={<CoursesSearch />} />
<Route path=":course">
<Route path="/" element={<CourseOverview />} />
<AuthRoute path="project">
<AuthRoute path="/" element={<CourseProject />} />
<AuthRoute path=":part" element={<ProjectPart />} />
</AuthRoute>
<AuthRoute path=":lesson">
<AuthRoute path="/" element={<CourseLesson />} />
<AuthRoute path="complete" element={<CourseLessonComplete />} />
<AuthRoute path=":concept" element={<CourseConcept />} />
</AuthRoute>
</Route>
</Route>
<Route path="policy" element={<PolicyAndTerms />} />
<Route path="terms" element={<PolicyAndTerms />} />
<Route path="*" element={<NoMatch />} />
</Routes> If you look real quick at all Is there some particular reason you wouldn't want data to be able to be passed as a prop on an Adding a reference to this other issue with the same question: #7590 |
In this case: why don't you use a custom context? |
Meaning there is no way to pass props from Parent to Child in a declarative routing system? // routes.ts
export const routes = [
{
path: '/*',
element: <Parent />,
children: [
{
path: 'child1',
elements: <Child1 />, // Property 'firstName' is missing in type '{}' but required in type 'IProps'
},
{
path: 'child2',
elements: <Child2 />, // Property 'lastName' is missing in type '{}' but required in type 'IProps'
}
]
}
]
// Parent.ts
const Parent = () => {
const firstName = 'John'
const lastName = 'Doe'
return (
<h1>Parent Component</h1>
<Outlet /> // How to pass the appropriate props?
)
}
// child1/2.ts
interface IFirstNameProps {
firstName: string
}
interface ILastNameProps {
lastName: string
}
export const Child1 = (props: IProps) => {
return (
<h2>First Name</h2>
{props.firstName}
)
}
export const Child2 = (props: IProps) => {
return (
<h2>Last Name</h2>
{props.lastName}
)
} |
Stumbled across this myself today. So instead of using the Outlet, one is supposed to pass everything through the Use Case:
Users:
User:
I think the concept of Descendant Routes would help me here, however, I would love to declare all routes at the top and use the Outlet as a tunnel instead. |
In case anyone is looking for a nice solution, do this with context:
Now you'd use FWIW, Michael and Ryan are probably going to make something (maybe similar to this) available in RR later (based on some chatter I'm seeing) |
Thanks @bradwestfall for providing a work around! I hope this will become native in RR6 though :) |
@bradwestfall nice |
For consistency (with the context API) I would call the prop Btw: exactly the solution I was thinking about 👍 |
We definitely have plans to allow passing stuff through outlet. Sorry @timdorr, I should've let you know before you closed this. Right now we're thinking this will look something like: <Outlet context={whateverYouWant} />
function ChildRoute() {
let stuff = useOutletContext();
// ...
} |
@bradwestfall nice and Thanks BUT::
assuming this in a file called Outlet.js
How do I then import or use the useOutletContext Part? |
Just throwing out a potential alternative solution using I'm happy to make a PR for this pattern if it's a direction you support @mjackson @timdorr.
|
@mikel-codes You would just import @arinthros I tried that too but it doesn't work with the API's we're given. We can call React Router's There's probably an internal way for React Router to do it internally, but I don't think that's the approach they want. I don't want to speak for Michael and Ryan directly, but based on conversations Ive had with them and seen them have with others, there's drawbacks to that approach. I don't think it's a lot less cognitive noise either. From a developer's standpoint, one approach means receiving props and the other means calling a hook, that's the only real difference |
This is now possible with |
@timdorr thanks for making this happen! Is there any documentation already there which we can read up? |
Also worth mentioning since I didnt see it in the
|
Quick question, if my sub routes expect different props from the parent route, what's the best way of passing what they specifically need? should I send all the props through the context and let the consumer sub routes pick what they need or is there any other alternative I'm not considering? thanks in advance |
@SKOLZ Take a look at const rootData = useMatches()[0].data
const leafData = useMatches().at(-1).data |
My current project's previous team had used a lot of cloneElement to pass props to nested routes.
So I got a tons of code to refactor now, and I do not see a way to migrate this code to v6. Do you consider way to allow pass props like
<Outlet myProp=1/>
, to pass it into underlying route's component?I'll give you an example to be sure
The text was updated successfully, but these errors were encountered: