Sherry
Sherry

Reputation: 47

Get data to export csv file using react-csv

I am trying react-csv. https://www.npmjs.com/package/react-csv

This is data:

users = [
{firtstname: 'Ahmed', lastname: 'Tomi' , email: '[email protected]'},
{firtstname: 'Raed', lastname: 'Labes' , email: '[email protected]'},
{firstname: 'Yezzi', lastname: 'Min l3b', email: '[email protected]'}
];

Data for react-csv:

<CSVLink data={this.props.users} >Download me</CSVLink>

Then, how do I get only lastname and email for csv. I try to do like this but something went wrong.

handleExport = () => {
this.state.exportData = this.props.users.map((user, index) => (
user.lastname + user.email
));
}

Thanks.

Upvotes: 1

Views: 7048

Answers (2)

Michael McCabe
Michael McCabe

Reputation: 1152

Two choices. Either map your data so it only contains what you need. Something like

<CSVLink data={this.props.users.map(x => ({ lastname: x.lastname, email: x.email }))}>Download me</CSVLink>

Or, use the headers prop to define the data you want

headers = [
  { label: 'Last Name', key: 'lastname' },
  { label: 'Email', key: 'email' }
];

<CSVLink data={this.props.users} headers={headers}>Download me</CSVLink>

Upvotes: 3

realkyo
realkyo

Reputation: 21

<CSVLink data={this.state.exportData} >Download me</CSVLink>

Upvotes: 0

Related Questions