O3 Framework / useOpenmrsSWR
useOpenmrsSWR<
DataType,ErrorType>(key,options):SWRResponse<FetchResponse<DataType>,ErrorType,undefined|SWRConfiguration<FetchResponse<DataType>,ErrorType,BareFetcher<FetchResponse<DataType>>>>
Defined in: packages/framework/esm-react-utils/src/useOpenmrsSWR.ts:70
Beta
This hook is intended to simplify using openmrsFetch in useSWR, while also ensuring that all useSWR usages properly use an abort controller, so that fetch requests are cancelled if the React component unmounts.
DataType = any
ErrorType = any
The SWR key to use
UseOpenmrsSWROptions = {}
An object of optional parameters to provide, including a FetchConfig object to pass to openmrsFetch or options to pass to SWR
SWRResponse<FetchResponse<DataType>, ErrorType, undefined | SWRConfiguration<FetchResponse<DataType>, ErrorType, BareFetcher<FetchResponse<DataType>>>>
import { useOpenmrsSWR } from "@openmrs/esm-framework";
function MyComponent() {
const { data } = useOpenmrsSWR(key);
return (
// render something with data
);
}
Note that if you are using a complex SWR key you must provide a url function to the options parameter,
which translates the key into a URL to be sent to openmrsFetch()
import { useOpenmrsSWR } from "@openmrs/esm-framework";
function MyComponent() {
const { data } = useOpenmrsSWR(['key', 'url'], { url: (key) => key[1] });
return (
// render something with data
);
}