Soy nuevo en React js.
Estoy tratando de exportar los datos tabulares devueltos a un archivo csv con un clic de botón.
let names = [ {firstName: 'John',lastName: 'Cena'}, {firstName: 'Rey',lastName: 'Mysterio'}, ] const App = props => { return ( <div> {names.length > 0 && <table> //table renders only when array has elements <tbody> <tr> <th>First Name</th> //setting headers <th>Last Name</th> </tr> {names.map((name,index) => { //mapping for individual rows return ( <tr key={index}> <td>{name.firstName}</td> <td>{name.lastName}</td> </tr> ) })} </tbody> </table>} </div> ); };Quiero crear un botón llamado exportar, y luego debería descargar los datos de la tabla que se devuelven a un archivo csv.
Cualquier ayuda es muy apreciada. Gracias.
Una biblioteca como react-csv lo ayudará a exportar datos a un archivo csv fácilmente, aquí hay un ejemplo:
import { CSVLink, CSVDownload } from "react-csv"; let names = [ {firstName: 'John',lastName: 'Cena'}, {firstName: 'Rey',lastName: 'Mysterio'}, ] const App = props => { return ( <> <CSVLink data={names}>Download me</CSVLink>; // or <CSVDownload data={names} target="_blank" />; </> ); };Para obtener más información sobre la biblioteca, visite: https://www.npmjs.com/package/react-csv