svelte-drama/swr

Initial value

Closed this issue · 1 comments

I'd like to provide some initial value to make model.live not return undefined while loading.

const model = swr({
  key: ...
  fetcher: () => await fetch(...),
  initialValue: 'hello',
});

 // type: Readable<string>, not Readable<string | undefined>
$: data = model.live(); 

There are a few ways to accomplish this currently:

  • If an initial value is known, use model.update to set it
  • Check $data ?? initial_value in your template
  • Wrap data in a derived store. You can return the value you need from then.