简体   繁体   English

csv-react & react-hook-form

[英]csv-react & react-hook-form

Good evening folks, I am stuck with this code for hours, and I don't understand how to figure it out.晚上好,伙计们,我被这段代码困了几个小时,我不明白如何弄清楚。 I want to retrieve data from the form to convert it into CSV file or Excel table in other words.我想从表单中检索数据以将其转换为 CSV 文件或 Excel 表格。 I used third-party libraries like CSV-react and react-hook-form.我使用了第三方库,例如 CSV-react 和 react-hook-form。 I would be grateful if you help me, thank you.如果你能帮助我,我将不胜感激,谢谢。

import React from "react";
import { useForm } from "react-hook-form";
import { CSVLink } from "react-csv";


export default function App() {
  const { register, handleSubmit } = useForm();
  const headers = [
    { label: "Address", key: "address" },
    { label: "T-Shirt Size", key: "size" },
    { label: "mail", key: "mail" },
  ];
  let data = [];
 
  const onSubmit = data => {
    console.log(data);
  };
  

   
  return (  
    <form onSubmit={handleSubmit(onSubmit)}>
      <label>Address</label>
      <input {...register("address")} />
      <label>T-Shirt Size</label>
      <select {...register("size")}>
        <option value="S">S</option>
        <option value="M">M</option>
        <option value="L">L</option>
        <option value="XL">XL</option>
      </select>
      <label>mail</label>
      <input {...register("mail")} />
      <label>       
        <input type="checkbox" required / >
Agreement to the Data Privacy Policy</label>


     <CSVLink data={data} headers={headers}>
  Download me
     </CSVLink>;
      <input type="submit" />
    </form>

  );
}

So, I find a solution in a forum, and I wanted to share it with you.所以,我在一个论坛里找到了一个解决方案,我想和你分享。

  import React, { useState } from "react";
  import { useForm } from "react-hook-form";
  import { CSVLink } from "react-csv";


  export default function App() {
    const { register, handleSubmit } = useForm();
    const [csvData, setCsvData] = useState([])

    const headers = [
      { label: "Address", key: "address" },
      { label: "T-Shirt Size", key: "size" },
      { label: "mail", key: "mail" },
    ];
  
    const onSubmit = data => {
      //console.log(data);
      setCsvData(prev => [...prev, data])
    };
    

    
    return (  
      <form onSubmit={handleSubmit(onSubmit)}>
        <label>Address</label>
        <input {...register("address")} />
        <label>T-Shirt Size</label>
        <select {...register("size")}>
          <option value="S">S</option>
          <option value="M">M</option>
          <option value="L">L</option>
          <option value="XL">XL</option>
        </select>
        <label>mail</label>
        <input {...register("mail")} />
        <label>       
          <input type="checkbox" required / >
  Agreement to the Data Privacy Policy</label>


      <CSVLink data={csvData} headers={headers}>
    Download me
      </CSVLink>;
        <input type="submit" />
      </form>

    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM