React-pdf提交多个值

我正在尝试创建一种表单,在这种情况下,您需要输入一些值,名称和姓氏,然后生成一个pdf。 我正在使用https://react-pdf.org/advanced#on-the-fly-rendering来完成任务。但是,我只能提交一个值。代码摘录如下:

<BlobProvider
          document={MyDoc({
            value: this.state.value,},{
            value1: this.state.value1,})}
        >
          {({ url }) => (
            <a href={url} target="_blank">
              Print
            </a>
          )}
        </BlobProvider>

在引入第二个值时,我尝试了多个选项,当console.log出现时,第二个值似乎未定义,因此它不能工作,更不用说更大的形式了。 这是代码https://codesandbox.io/s/strange-ramanujan-847ph?file=/src/App.js

的全部内容

抱歉,我没有设法使其在 codesandbox 中起作用,但是在我的代码编辑器中确实起作用。

iCMS 回答:React-pdf提交多个值

设法使其起作用,是的,您可以引入尽可能多的值

 <BlobProvider
          document={MyDoc({
            value: this.state.value,surname: this.state.surname,**more values here**
          })}
        >
          {({ url }) => (
            <a href={url} target="_blank">
              Print
            </a>
          )}
        </BlobProvider>

以下代码的完整范围:

import React from "react";
import { BlobProvider,Document,Page,Text,View } from "@react-pdf/renderer";

const MyDoc = ({ value,surname }) => (
  <Document>
    <Page wrap>
      <Text>Section #1</Text>

      <View style={{ flexDirection: "row",margin: "2",color: "blue" }}>
        <Text style={{ padding: 10 }}>Name : </Text>
        <Text style={{ padding: 10 }}>{value}</Text>
        {console.log("name",value)}
      </View>

      <View style={{ flexDirection: "row",color: "blue" }}>
        <Text style={{ padding: 10 }}>Surname : </Text>
        <Text style={{ padding: 10 }}>{surname}</Text>
        {console.log("surname",surname)}
      </View>
    </Page>
  </Document>
);

class App extends React.Component {
  state = { value: "",surname: "" };

  render() {
    return (
      <div >
        <div>Pdf Generator</div>
        <form>
          <div >
            <div >
              <label>name</label>
              <input

                value={this.state.value}
                type="text"
                onChange={(e) => this.setState({ value: e.target.value })}
                placeholder="name"
              />
            </div>

            <div>
              <label>surname</label>
              <input
                value={this.state.surname}
                type="text"
                onChange={(e) => this.setState({ surname: e.target.value })}
                placeholder="surname"
              />
            </div>
          </div>
        </form>

        <BlobProvider
          document={MyDoc({
            value: this.state.value,})}
        >
          {({ url }) => (
            <a href={url} target="_blank">
              Print
            </a>
          )}
        </BlobProvider>
      </div>
    );
  }
}

export default App;
本文链接:https://www.f2er.com/2253174.html

大家都在问