A simple and easy-to-use React hook for exporting data as CSV. Works everywhere: Web, React Native, Next.js, Node.js, SSR.
- Simple API: Just 3 functions:
download
,copy
,csvString
- Universal: Works on Web, React Native, Next.js, Node.js, Edge Runtime, SSR
- Zero Dependencies: Lightweight with no external packages
- TypeScript: Full type safety and IntelliSense support
- SSR Safe: Works in server-side rendering environments
npm install react-csv-export-hook
# or
yarn add react-csv-export-hook
# or
pnpm add react-csv-export-hook
import { useCsvExport } from 'react-csv-export-hook';
function MyComponent() {
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'Los Angeles' }
];
const { download, copy, csvString } = useCsvExport(data, 'users');
return (
<div>
<button onClick={download}>Download CSV</button>
<button onClick={copy}>Copy to Clipboard</button>
<p>CSV: {csvString}</p>
</div>
);
}
That's it! π
import { useCsvExportNext } from 'react-csv-export-hook';
function NextJSComponent() {
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'Los Angeles' }
];
const { download, copy, csvString, isReady } = useCsvExportNext(data, 'users');
return (
<div>
{!isReady && <p>Loading...</p>}
{isReady && (
<>
<button onClick={download}>Download CSV</button>
<button onClick={copy}>Copy to Clipboard</button>
</>
)}
<p>CSV: {csvString}</p>
</div>
);
}
import { useCsvExportNative } from 'react-csv-export-hook';
function NativeComponent() {
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'Los Angeles' }
];
const { copy, csvString } = useCsvExportNative(data);
return (
<div>
<Button title="Copy CSV" onPress={copy} />
<Text>CSV: {csvString}</Text>
</div>
);
}
import { useCsvExport } from 'react-csv-export-hook';
function ServerComponent() {
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'Los Angeles' }
];
const { csvString, getBuffer } = useCsvExport(data, 'users');
// Use in server environments
const handleServerExport = () => {
const buffer = getBuffer();
if (buffer) {
// Send as response or save to file
console.log('Server buffer created:', buffer);
}
};
return (
<div>
<button onClick={handleServerExport}>Create Server Buffer</button>
<p>CSV: {csvString}</p>
</div>
);
}
Parameters:
data: T[]
- Array of objects to exportfileName: string
- Name for the CSV file (without .csv extension)
Returns:
download()
- Downloads CSV file (browser only)copy()
- Copies CSV to clipboardcsvString: string
- Generated CSV stringgetBuffer()
- Returns Uint8Array for server-side usage
Returns:
- All features from
useCsvExport
isReady: boolean
- True when hydrated and ready
Returns:
copy()
- Copy CSV to clipboardcsvString: string
- Generated CSV stringgetBuffer()
- Returns Uint8Array for server-side usage
Environment | Download | Copy | String | Buffer |
---|---|---|---|---|
Web Browser | β | β | β | β |
React Native | β | β | β | β |
Next.js SSR | β | β | β | β |
Node.js | β | β | β | β |
Edge Runtime | β | β | β | β |
function AdvancedComponent() {
const [data, setData] = useState([]);
const { download, copy, csvString, getBuffer } = useCsvExport(data, 'export');
const handleDownload = () => {
if (data.length === 0) {
alert('No data to export!');
return;
}
download();
};
const handleCopy = async () => {
try {
await copy();
alert('CSV copied to clipboard!');
} catch (error) {
alert('Failed to copy CSV');
}
};
const handleServerExport = () => {
const buffer = getBuffer();
if (buffer) {
// Use buffer in API call
fetch('/api/export', {
method: 'POST',
body: buffer
});
}
};
return (
<div>
<button onClick={handleDownload} disabled={data.length === 0}>
Download CSV ({data.length} rows)
</button>
<button onClick={handleCopy}>Copy to Clipboard</button>
<button onClick={handleServerExport}>Server Export</button>
</div>
);
}
function DynamicComponent({ users, fileName }) {
const { download, copy, csvString } = useCsvExport(users, fileName);
return (
<div>
<p>Exporting {users?.length || 0} users</p>
<button onClick={download} disabled={!users?.length}>
Download {fileName}.csv
</button>
<button onClick={copy} disabled={!users?.length}>
Copy CSV
</button>
</div>
);
}
- SSR Safe: No DOM access during server-side rendering
- Hydration Aware: Waits for client hydration before DOM operations
- Error Handling: Graceful fallbacks for unsupported operations
- Type Safe: Full TypeScript support
The library is fully backward compatible. New features are additive:
// Old usage still works
const { exportCsv, csvString } = useCsvExport(data, 'export');
// New simplified usage
const { download, copy, csvString } = useCsvExport(data, 'export');
// Use the new functions
download(); // Instead of exportCsv()
copy(); // New: Copy to clipboard
MIT License - see LICENSE file for details.
Contributions are welcome! Please read our contributing guidelines.
- π¬ Discussions
Simple. Universal. Powerful. π