Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data is undefined using reactive query #17

Open
frederikhors opened this issue Aug 11, 2021 · 8 comments
Open

data is undefined using reactive query #17

frederikhors opened this issue Aug 11, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@frederikhors
Copy link
Contributor

frederikhors commented Aug 11, 2021

Using the below code it makes data === undefined. Why?

REPL: https://svelte.dev/repl/40782b4783244075b8c58cffbcf9acd4?version=3.42.1.

<script lang="ts">
  import { useSWR } from "sswr";

  let pagination = defaultPagination;

  $: ({ data, error } = useSWR(`players`, {
    fetcher: () => Promise.resolve(service.playerList({ pagination })),
  }));

  $: console.log("$error:", $error);
  $: console.log("$data:", $data); // $data === undefined as soon as I click Next page button

  function nextPage() {
    pagination = { ...pagination, page: $data.pageInfo.currentPage + 1 };
  }
</script>

<button on:click={nextPage}>Next page</button>

{#if $error} {$error.message} {:else if !$data} Loading... {:else}
<ul>
  {#each $data.players as player (player.id)}
  <li>
    <a href="/players/{player.id}">{player.id}</a>
  </li>
  {:else}
  <h1>No players</h1>
  {/each}
</ul>
{/if}
@frederikhors frederikhors changed the title data === undefined using reactive query data is undefined using reactive query Aug 11, 2021
@ConsoleTVs
Copy link
Owner

Can I know more about what service.playerList does under the hood?

@frederikhors
Copy link
Contributor Author

Can I know more about what service.playerList does under the hood?

In the REPL for this issue I used a simple fetch with the same issue: https://svelte.dev/repl/40782b4783244075b8c58cffbcf9acd4?version=3.42.1.

My service is similar.

@frederikhors
Copy link
Contributor Author

THIS IS HUGE and prevent me from using sswr today.

@ConsoleTVs
Copy link
Owner

I'm going to explore this after work. Thanks for the report.

@Terrahop
Copy link

Terrahop commented Aug 26, 2021

Having the same issue here with trying to get pagination to work.

Given the following code in a svelte component:

  /** Item position to start at */
  let offset = 0
  /** Number of items to get */
  let first = 10

  $: ({ data: pageData } = useSWR(() => 'pageItems', {
    fetcher: () => query('someItemList', { first, offset })
  }))

  $: console.log('page data', $pageData)

pageData is initially populated with data however when I update offset via a button press (offset += first), pageData becomes undefined and then no longer updates either on further changes to offset.

The same behaviour happens when not using a custom fetcher:

{...}
  $: ({ data: pageData } = useSWR(() => `api/someItemList?first=${first}&offset=${offset}`))
{...}

@benbender
Copy link
Contributor

benbender commented Sep 4, 2021

@Terrahop You are simply rerunning useSWR reactivly with different parameters. Therefor data is initialized as undefined at the beginning of the new request as per docs. See:

data: Writable<D | undefined>: Stores the data of the HTTP response after the fetcher has processed it or undefined in case the HTTP request hasn't finished or there was an error.

So imho this is expected behaviour.

The solution to this is either:

  • save the old data in an intermediate variable until new data arrives (outside of useSWR())
  • extend the api of useSWR with something like keepData: boolean.

@Terrahop
Copy link

Ah got it thanks for clearing that up, I'll take a look at extending useSWR then.

@akiarostami
Copy link

I'm facing this issue as well, which is preventing me from using useSWR, unfortunately.

@ConsoleTVs ConsoleTVs self-assigned this Nov 21, 2021
@ConsoleTVs ConsoleTVs added the bug Something isn't working label Nov 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants