Swr: Is the advantage of SWR only in the first fetch?

Created on 3 Dec 2019  路  2Comments  路  Source: vercel/swr

If we get data through events, what's the advantage of swr

question

Most helpful comment

I'm not sure what does "in the first fetch" mean, could you explain?

As for the second question, if you use event emitter to handle data fetching, you'll have to do more work to:

  • subscribe event listeners for each component
  • unsubscribe them when unmounting
  • get the data on render
  • deduplicate requests
  • ...

For complex web apps, e.g.: say a page with 100 component instances, both of them require the data of some API, how can you make sure:

  • they don't make duplicate requests
  • their state are always consistent
  • they will be re-rendered at the same time, ASAP, if the data changes
  • they're still easy to maintain

If you try to simplify those processes with hooks, it will probably result in something similar to swr at the end.

Meanwhile, I'm working on a complex, real world SWR example too.

All 2 comments

I'm not sure what does "in the first fetch" mean, could you explain?

As for the second question, if you use event emitter to handle data fetching, you'll have to do more work to:

  • subscribe event listeners for each component
  • unsubscribe them when unmounting
  • get the data on render
  • deduplicate requests
  • ...

For complex web apps, e.g.: say a page with 100 component instances, both of them require the data of some API, how can you make sure:

  • they don't make duplicate requests
  • their state are always consistent
  • they will be re-rendered at the same time, ASAP, if the data changes
  • they're still easy to maintain

If you try to simplify those processes with hooks, it will probably result in something similar to swr at the end.

Meanwhile, I'm working on a complex, real world SWR example too.

Let me know if you have any further questions!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timurmaio picture timurmaio  路  3Comments

Svish picture Svish  路  5Comments

tiagocorreiaalmeida picture tiagocorreiaalmeida  路  3Comments

DoT214 picture DoT214  路  4Comments

alexanderbluhm picture alexanderbluhm  路  3Comments