You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the new or updated feature that you are suggesting?
Proposal: Update the element prop of the Route component so it also accepts a function. If provided, the function is given the loader data.
functionSomeComponent(){return(<Routepath="/user/:id"loader={({params})=>loadUser(params.id)}element={({ data })=>(// ...)}/>);}
Why should this feature be included?
This proposal aims at achieving similar results than #9189, but with a more "type safe" approach.
Currently, to access loader data you need to use the useLoaderData hook. The usage of a hook makes Typescript lose track of the type of the data. If Route provides the data to its element prop, then react-router can make use of generics to ensure that type information is not lost.
If loader returns a Promise<Response> or Response, and if element is a function, then we'll have to trust that the deserialized data will be of type T.
I do not have the full picture about the ins and outs of useLoaderData, so I'm aware that my proposal may not be in line with react-router's architecture.
EDIT 2023-01-04
I just stumbled upon the FAQ that explains why Route no longer accepts render props. I understand the motivation behind this choice and I don't expect the Remix team to make this issue a priority.
However I'm sure the team is aware that using react-router's hooks is not the panacea. Not just for the type-safety issue mentioned above. As Fiona described below, they tend to add more coupling between your components and react-router. I can only hope you'd reconsider the tradeoffs of render-props :)
The text was updated successfully, but these errors were encountered:
This is also appealing because components that call useLoaderData are trickier to test using things like Storybook owing to needing to mock how to get data in there.
For my project I’ve made a typesafe helper to achieve the same as the above feature request:
What is the new or updated feature that you are suggesting?
Proposal: Update the
element
prop of theRoute
component so it also accepts a function. If provided, the function is given the loader data.Why should this feature be included?
This proposal aims at achieving similar results than #9189, but with a more "type safe" approach.
Currently, to access loader data you need to use the
useLoaderData
hook. The usage of a hook makes Typescript lose track of the type of the data. IfRoute
provides the data to itselement
prop, then react-router can make use of generics to ensure that type information is not lost.Before
After
I do not have the full picture about the ins and outs of
useLoaderData
, so I'm aware that my proposal may not be in line with react-router's architecture.EDIT 2023-01-04
I just stumbled upon the FAQ that explains why
Route
no longer accepts render props. I understand the motivation behind this choice and I don't expect the Remix team to make this issue a priority.However I'm sure the team is aware that using react-router's hooks is not the panacea. Not just for the type-safety issue mentioned above. As Fiona described below, they tend to add more coupling between your components and react-router. I can only hope you'd reconsider the tradeoffs of render-props :)
The text was updated successfully, but these errors were encountered: