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.

Feature request: Lazy fetcher ?

See original GitHub issue

Imagine a Cascader component, which has two Select components, in which the second one depends on the selected item from the first component.

In this case, could useSWR supports lazy fetcher by returning a refetch function which can be run later ?

const { data, error, refetch } = useSWR(key, { lazy: true })

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
ismaycommented, Apr 22, 2021

You can use mutate in that case right?

const { data } = useSWR('select-data')

return <>
  {data}
  <button onClick={() => {
    mutate('select-data', fetch('/api'))
  }}>lazy fetch</button>
</>

In fact, mutate is so flexible that can be used in both prefetching and lazy fetching.

To me this does not really resolve the issue. I know that technically this addresses the feature request, but it doesn’t seem like an officially supported solution to me. All I see in the docs for mutate is references to mutating data (which makes sense), not lazy fetching.

The fact that it can be used for lazy fetching seems more like an implementation detail to me than an offically supported part of the API. I’d be hesistant to use this in production as I wouldn’t feel comfortable relying on this not breaking in a patch/minor bump.

Could we reopen, and ideally implement a lazy flag? Would that be in scope for this lib?

8reactions
good-ideacommented, Jul 13, 2020

EDIT: see my gist below with a better solution

I agree that a lazy option would be nice, but for now, here is my solution for a useLazyRequest hook:

const request = async () => {
  // whatever your request function is
};

type LazyRequestTuple<R, V extends Variables = Variables> = [
  (v: V) => Promise<void>,
  ResponseInterface<R | null, Error>
];

export const useLazyRequest = <R, V extends Variables = Variables>(
  query: DocumentNode
): LazyRequestTuple<R, V> => {
  const response = useSWR<R | null>(print(query), () => {
    return null;
  });

  const executeQuery = async (variables?: V) => {
    const result = await request<R>(query, variables);
    response.mutate(result, false);
  };

  return [executeQuery, response];
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request: Lazy fetcher ? · Issue #176 · vercel/swr - GitHub
In this case, could useSWR supports lazy fetcher by returning a refetch function which can be run later ? const { data, error,...
Read more >
5 ways to initialize lazy associations and when to use them
Using fetch joins in JPQL statements can require a huge number of queries, which will make it ... Named entity graphs are a...
Read more >
Eager/Lazy Loading In Hibernate - Baeldung
It can result in fetching and storing a lot of data, irrespective of the need for it. We can use the following method...
Read more >
Lazy fetching in Spring JPA - different behaviour within cron ...
The error tell you that you're trying to fetch a lazy association, ... of short-lived Session and bypasses a lot of features that...
Read more >
The best way to lazy load entity attributes using JPA and ...
This configuration alone is not sufficient because Hibernate requires bytecode instrumentation to intercept the attribute access request 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