【问题标题】:Hi i am new to React js can you help me convert this class component to functional component嗨,我是 React js 的新手,你能帮我把这个类组件转换为函数式组件吗
【发布时间】:2022-01-02 17:36:35
【问题描述】:

你好,这是下面的代码,如何在 React js 中将这个类组件转换为函数式组件。 嗨,我是 React js 的新手,您能帮我将此类组件转换为功能组件吗?我正在尝试创建一个单击该按钮应使用 jspdf 和 jspdf 自动表生成 pdf 的按钮

    import React from 'react';
    import jsPDF from "jspdf";
    import "jspdf-autotable";
    import './App.css';
    
    class App extends React. Component {
    
      constructor() {
        super();
        this.state = {
          people: [
            { name: "Keanu Reeves", profession: "Actor" },
            { name: "Lionel Messi", profession: "Football Player" },
            { name: "Cristiano Ronaldo", profession: "Football Player" },
            { name: "Jack Nicklaus", profession: "Golf Player" },
          ]
        }
      }
    
      exportPDF = () => {
        const unit = "pt";
        const size = "A4"; // Use A1, A2, A3 or A4
        const orientation = "portrait"; // portrait or landscape
    
        const marginLeft = 40;
        const doc = new jsPDF(orientation, unit, size);
    
        doc.setFontSize(15);
    
        const title = "My Awesome Report";
        const headers = [["NAME", "PROFESSION"]];
    
        const data = this.state.people.map(elt=> [elt.name, elt.profession]);
    
        let content = {
          startY: 50,
          head: headers,
          body: data
        };
    
        doc.text(title, marginLeft, 40);
        doc.autoTable(content);
        doc.save("report.pdf")
      }
    
      render() {
        return (
          <div>
            <button onClick={() => this.exportPDF()}>Generate Report</button>
          </div>
        );
      }
    }
    
    export default App;

【问题讨论】:

  • 请根据您所做的任何尝试更新您的问题
  • const [exportPDF, setExportPDF] = useState(true);
  • exportPDF = () => { const unit = "pt";常量大小=“A4”; // 使用 A1、A2、A3 或 A4 constorientation = "portrait"; // 纵向或横向 const marginLeft = 40; const doc = new jsPDF(方向,单位,大小); doc.setFontSize(15); const title = "导出 PDF";常量头= [[“标签”,“名称”]];常量数据 = gridData.map(elt=> [elt.label, elt.name]);让 content = { startY: 50, head: headers, body: data }; doc.text(title, marginLeft, 40); doc.autoTable(内容); doc.save("report.pdf")
  • 你能用所有这些来编辑问题,而不是把它放在 cmets 中吗?

标签: javascript reactjs class functional-programming components


【解决方案1】:
 import React from 'react';
    import jsPDF from "jspdf";
    import "jspdf-autotable";
    import './App.css';
    
    const App =()=> {
     const [state,setState] =React.useState {
          people: [
            { name: "Keanu Reeves", profession: "Actor" },
            { name: "Lionel Messi", profession: "Football Player" },
            { name: "Cristiano Ronaldo", profession: "Football Player" },
            { name: "Jack Nicklaus", profession: "Golf Player" },
          ]
        }
    
      const exportPDF = () => {
        const unit = "pt";
        const size = "A4"; // Use A1, A2, A3 or A4
        const orientation = "portrait"; // portrait or landscape
    
        const marginLeft = 40;
        const doc = new jsPDF(orientation, unit, size);
    
        doc.setFontSize(15);
    
        const title = "My Awesome Report";
        const headers = [["NAME", "PROFESSION"]];
    
        const data = state?.people?.map(elt=> [elt.name, elt.profession]);
    
        let content = {
          startY: 50,
          head: headers,
          body: data
        };
    
        doc.text(title, marginLeft, 40);
        doc.autoTable(content);
        doc.save("report.pdf")
      }
    
        return (
          <div>
            <button onClick={exportPDF}>Generate Report</button>
          </div>
        );
      
    }
    
    export default App;

【讨论】:

  • people变量可以是普通变量而不是状态变量。
  • @ManishKumar 是的,可以,但主要关心的是把他的 Class 组件转换成函数式组件
猜你喜欢
  • 2022-01-24
  • 2023-01-22
  • 1970-01-01
  • 2020-07-11
  • 2021-10-14
  • 2022-12-17
  • 2020-06-16
  • 2021-08-29
  • 1970-01-01
相关资源
最近更新 更多