fetch data logic in route components
See original GitHub issueEnhancement
Suggestion by
Reason to enhance/problem with existing solution
- Executing asynchronous API calls in containers never seemed to be the right place, particularly because it’s still a view, it should not be it’s concern (imo).
- Making a component fetch data through static methods feels hacky. It breaks down when trying to wrap this top level component with page level components, take a look at this. Our prerenderMiddleware won’t be able to find the
fetchData
method because it is no longer the top level component. - Because Mehdi “The Don” Mollaverdi said so 😉
Suggested enhancement
<Route path="/a" fetch={fetchSpecificRouteData} component={SpecificRouteComponent} />
If we have a single fetchData
method for each route, we no longer need a Promise.all
in pre-render middleware because we only need to return a single promise.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Data Fetching | Vue Router
Let's assume we have a Post component that needs to fetch the data for a post based on $route.params.id : <template> <div class="post">...
Read more >Fetch data for the entire app without depending on component ...
No matter which route is visited, I need to use some data from an api. This data is constant through the application. Using...
Read more >React, routing, and data fetching | by Jimmy Jia - Medium
The ideal solution is to fetch all data and code in parallel after navigating to a new set of routes. For good UX,...
Read more >How to fetch data in React with performance in mind
Let's start with laying out components first, then wire the data fetching afterward. We'll have the app component itself, it will render Sidebar ......
Read more >Redux Essentials, Part 5: Async Logic and Data Fetching
We can use the posts.status enum to help decide if we need to actually start fetching, by selecting that into the component and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jrodl3r were you able to find a solution to pass URL params to the fetchData call in the Route component?
@choonkending can you explain why we want to fire “CREATE_REQUEST” + “REQUEST_SUCCESS” on every location change? I’m just trying to comprehend the strategy better… As my setup will still have components w/ their own fetching, etc. and the console is a little dirty I guess is my only gripe…
just noticed that is happening on every URL (even w/ { data: undefined }) and not sure if I should be concerned…
Also, is there anyway to pass/access
location.pathname
orparams
inside the service? (For example: get the ID url param, andif (params.id !== state.id) axios.get()
…or do nothing?)I do like having this setup for base container components (home, dashboard, landing pages, etc.) - Thx!