Skip to content

Fetch plugin

syncedFetch is a simple wrapper around fetch to reduce boilerplate.

  • get: The URL to get. If it is an observable or Selector function, it will re-run whenever the value changes.
  • set: The URL to set
  • getInit: The init parameter to pass to fetch when getting
  • setInit: The init parameter to pass to fetch when setting. Defaults to { method: 'POST' }
  • valueType: The function to call on the Response set. Defaults to json.
  • onSavedValueType: The function to call on the Response from set. Defaults to the valueType option or json.
  • onSaved: Given the return value from set, return a value to save back into the observable.

Example:

import { syncedFetch } from '@legendapp/state/sync-plugins/fetch';
import { observable } from '@legendapp/state';
const state$ = observable(syncedFetch({
get: 'https://url.to.get',
set: 'https://url.to.set',
onSaved: (value) => {
return {
updatedAt: value.updatedAt
}
}
}))