getRouteApi usage
is there an example on when to use the getRouteApi function? From what I understand, it's useful for when you want to access route's data, but the component/hook isn't in the same file as the Route definition.
Since getRouteApi() is called outside components, is it a good idea to group calls of this function for all the routes in my project in one file in the root of my project and then just use the returned values in the application for easier management?
4 Replies
extended-salmon•16mo ago
Not really, its more for needing it in components that dont reside in the same file as your route definition.
Like say, you have a CheckoutDetailsCard component that's rendered on the Checkout page.
You may want to move this component to a separate file for readability, preference, etc, but sill need access to route loader data, or search params, etc.
The getRouteApi function gets used here. It essentially has all the same hooks as whats returned by the Route configuration.
exotic-emeraldOP•16mo ago
yeah, I am already using it, was just asking if it's a good practice to put all these calls for every route inside one file (for better organization) instead of calling it inside every file where my components that need to use it's api live
extended-salmon•16mo ago
No, its better to just use it as and when you need it.
Dont put it all in the same file, since there are runtime checks that happen which could throw an exception.
exotic-emeraldOP•16mo ago
okay, thanks!