class Loadable
A Loadable
object represents the current state of a Recoil atom or selector. This state may either have a value available, may be in an error state, or may still be pending asynchronous resolution. A Loadable
has the following interface:
state
: The current state of the atom or selector. Possible values are'hasValue'
,'hasError'
, or'loading'
.contents
: The value represented by thisLoadable
. If the state ishasValue
, it is the actual value, if the state ishasError
it is theError
object that was thrown, and if the state isloading
, then it is aPromise
of the value.
Loadables also contain helper methods for accessing the current state. Consider this API to be unstable:
getValue()
- Method to access the value that matches the semantics of React Suspense and Recoil selectors. If the state has a value then it returns a value, if it has an error then it throws that error, and if it is still pending then it suspends execution or rendering to propagate the pending state.toPromise()
: returns aPromise
that will resolve when the selector has resolved. If the selector is synchronous or has already resolved, it returns aPromise
that resolves immediately.valueMaybe()
- Returns the value if available, otherwise returnsundefined
valueOrThrow()
- Returns the value if available or throws an Error.map()
- Takes a function to transform the value of the Loadable and returns a new Loadable with the transformed value. The transformation function gets a parameter of the value and returns the new value; it can also propagate thrown errors or suspense.
Example
function UserInfo({userID}) {
const userNameLoadable = useRecoilValueLoadable(userNameQuery(userID));
switch (userNameLoadable.state) {
case 'hasValue':
return <div>{userNameLoadable.contents}</div>;
case 'loading':
return <div>Loading...</div>;
case 'hasError':
throw userNameLoadable.contents;
}
}