Hooks

useDataExport

Client-side export of already-loaded rows to CSV, JSON, Markdown or Excel on React Native. Projects rows through a column list, writes the file into the cache directory and opens the share sheet. API-aligned with the web useDataExport.

iOSAndroidExpo Go

Installation

pnpm dlx @docyrus/cli add @docyrus/rn-hooks-use-data-export
Required Packages(2 packages)
pnpm add expo-file-system (optional) expo-sharing (optional)

The hook needs no Docyrus client. It works on rows you already hold, such as the rows of a list, a grid page or generated sample data. For a full server-side export of a data source, use useDocyrusDataExport.

expo-file-system (SDK 54+ File / Paths API) and expo-sharing are optional peers, loaded lazily through the internal lib/native-export helpers. Without expo-file-system, exportData resolves null and writes nothing. Without expo-sharing, the file is written but the share sheet does not open.

Usage

import { Button } from '@/components/docyrus-native/button';
import { useDataExport, type DataExportColumn } from '@/hooks/docyrus-native/use-data-export';

type Contact = { id: string; name: string; email: string; company?: { name: string } };

const columns: DataExportColumn<Contact>[] = [
  { id: 'name', header: 'Name' },
  { id: 'email', header: 'Email' },
  { id: 'company', header: 'Company', accessor: row => row.company?.name ?? '' }
];

export function ExportContacts({ rows }: { rows: Contact[] }) {
  const { exportData, isExporting } = useDataExport({ columns, fileName: 'contacts' });

  return (
    <Button loading={isExporting} onPress={() => void exportData(rows, 'xlsx')}>
      Export to Excel
    </Button>
  );
}

Formats

FormatExtensionOutput
csv.csvRFC 4180 quoting, CRLF line endings, UTF-8 BOM by default (csvBom).
json.jsonArray of projected objects keyed by column id, pretty-printed.
markdown.mdGitHub table. Pipes and backslashes are escaped and newlines become <br />.
xlsx.xlsxSingle sheet with a bold, frozen header row. Numbers, booleans and dates are typed cells, and objects are JSON strings.

Without a formatter, primitives are written as they are, dates as ISO strings, and objects / arrays through JSON.stringify.

API Reference

Options (UseDataExportOptions<TData>)

OptionTypeDefaultDescription
columnsDataExportColumn<TData>[]—Column projection. Order is kept in the output (required).
fileNamestring'export'File name without extension. Invalid characters are removed.
sheetNamestring'Sheet1'Sheet name for xlsx.
csvBombooleantruePrepend a UTF-8 BOM to CSV output so Excel detects the encoding.
sharebooleantrueNative: open the share sheet after writing. false only writes the file and returns its uri.
dialogTitlestringthe file nameNative: title of the Android share chooser.

DataExportColumn<TData>

FieldTypeDescription
idstringStable column id, also the JSON key (required).
headerstringHeader label for CSV, Markdown and Excel (required).
accessor(row: TData) => unknownRaw value. Defaults to row[id].
formatter(value: unknown, row: TData) => stringRuns after accessor to produce the written value.

Result (UseDataExportResult<TData>)

FieldTypeDescription
exportData(rows: TData[], format: DataExportFormat) => Promise<DataExportResult | null>Project, write and share. Resolves null when expo-file-system is missing.
isExportingbooleanTrue while a file is being written.

DataExportResult

FieldTypeDescription
mimeTypestringMIME type of the file.
extensionstringExtension including the leading dot.
fileNamestring<fileName><extension>.
uristringNative: local file:// URI in the cache directory.

Differences from web

  • Web builds a Blob and clicks an <a download>. Native writes the file into the cache directory and opens the share sheet, so the user can save it to Files, send it or open it in another app.
  • Web writes Excel with exceljs, which needs Node Buffer / stream polyfills that Metro does not ship. Native uses a zero-dependency OOXML writer (buildSimpleXlsx in the internal lib/native-export), which is also used by the pivot-grid exporter.
  • Native adds the share and dialogTitle options and the uri field on the result. exportData can resolve null.

Type Exports

TypeDescription
UseDataExportOptionsHook options.
UseDataExportResultHook result.
DataExportColumnColumn projection entry.
DataExportFormat'csv' | 'json' | 'markdown' | 'xlsx'.
DataExportResultWritten file metadata.

On this page