question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch data logic in route components

See original GitHub issue

Enhancement

Suggestion by

@mmollaverdi

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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vsc-githubcommented, Jan 24, 2017

@jrodl3r were you able to find a solution to pass URL params to the fetchData call in the Route component?

1reaction
jrodl3rcommented, Sep 20, 2016

@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 or params inside the service? (For example: get the ID url param, and if (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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found