Read a document

Read a document

The useFrappeGetDoc hook can be used to fetch a document from the database. The hook uses useSWR under the hood and it's configuration can be passed to it.

Parameters:

No.VariabletypeRequiredDescription
1.doctypestringName of the doctype
2.docnamestringName of the document
3.swrKeyKey-SWR Key
4.optionsSWRConfiguration-SWR Configuration Options
export const MyDocumentData = () => {
  const { data, error, isValidating, isLoading, mutate } = useFrappeGetDoc<T>(
    'User',
    'Administrator',
  );
 
  if (isLoading) {
    return <>Loading</>;
  }
  if (error) {
    return <>{JSON.stringify(error)}</>;
  }
  if (data) {
    return (
      <p>
        {JSON.stringify(data)}
        <button disabled={isValidating} onClick={() => mutate()}>Reload</button>
      </p>
    );
  }
  return null;
};