Skip to main content

api

import { api } from '@zedux/react'

A factory for creating AtomApis.

Examples

Miscellaneous:

import { api } from '@zedux/react'

const myApi = api()
const withValue = api('some value')
const withStore = api(injectStore())
const withExports = api(val).setExports({ ...myExports })
const withPromise = api(val).setPromise(myPromise)
const cloningApis = api(myApi).addExports(withExports.exports)
const addingExports = api(withExports).addExports({ ...moreExports })
const overwritingExports = api(withExports).setExports({ ...newExports })
const settingTtl = api().setTtl(0)
const ttlPromise = api().setTtl(myPromise)
const ttlObservable = api().setTtl(my$)

// query atoms:
return api(myPromise)

Signature

api = (value?, wrap?) => atomApi
value

Optional. Can be absolutely anything.

If a Zedux store is passed, it will be set as the returned api's .store property. Otherwise, .store will be undefined.

If a promise is passed and this api is returned from an atom state factory, the atom will become a query atom.

If another Atom API is passed, api() clones it - returning a new AtomApi instance with all the properties of the passed api copied over (shallowly).

wrap

Optional. A boolean. Default true.

Whether to automatically wrap all exported functions in ecosystem.batch() calls.

Zedux wraps all exported functions by default. Pass false to prevent this. This wrapping has some overhead, but is recommended to leave enabled unless you know what you're doing. Forgetting to batch updates can lead to state tearing in rare cases.

Returns
An AtomApi instance.

See Also