错误在reactjs中显示输入值时无法读取属性

玛丽亚姆

我收到此错误:TypeError:
当我尝试使用@ react-pdf / renderer包在pdf中显示输入值时,无法读取未定义的属性“ setState”
错误完全在:{this.setState.title}行中
,有人可以告诉我是什么原因导致此错误,在此先谢谢您。这是我的代码:

import React, { Component } from 'react';
import { PDFDownloadLink, Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

const styles = StyleSheet.create({
  page: {
    flexDirection: 'row',
    backgroundColor: '#E4E4E4'
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

// Create Document Component
const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>{this.setState.title}</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
    
  </Document>
);

class Form extends Component {
  state = { 
    title: '',
    
    url: null };

    onChange = input => e => {
        this.setState({
            [input]: e.target.value
        });
    }

  onRender = ({ blob }) => {
    this.setState({ url: URL.createObjectURL(blob) });
  };

  render() {
    return (
        <div>
            <input onChange={this.onChange('title')} name="title" type="text" placeholder="Post Title" className="form-control" />
            Download your PDF here:
            <PDFDownloadLink
                document={<MyDocument/>}
                fileName="doc.pdf">
                    {({ blob, url, loading, error }) => (
                        loading ? 'Loading...' : 'Download!'
                    )}
            </PDFDownloadLink>
        </div>
    );
  }
}

export default Form;
开发者爱玛

现在的问题是在功能组件中使用它

const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>{this.setState.title}</Text>    /// this point
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
    
  </Document>
);

您应该将此值作为对此组件的支持传递给

const MyDocument = (props) => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>{props.title}</Text>    /// this point
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
    
  </Document>
);

并通过标题作为道具

<PDFDownloadLink
                document={<MyDocument title={this.state.title}/>}
                fileName="doc.pdf">
                    {({ blob, url, loading, error }) => (
                        loading ? 'Loading...' : 'Download!'
                    )}
            </PDFDownloadLink>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

javascript错误:多个输入框上的输入微调框上显示“无法读取null的属性'值'”

类型错误:无法读取未定义的属性“url”,无法从 reactjs 中的数组调用某些值

ReactJS:获取所选值时无法读取null的属性“值”

在 ReactJs 中单击按钮时,它显示“无法读取未定义的属性编辑名称”

类型错误:无法读取 null 的属性“值”| reactjs |

TypeError:无法读取REACTJS中未定义的属性“值”

无法读取表单中未定义错误的属性“值”

无法读取空输入字段的属性“值”?

无法在输入字段中输入值reactjs

错误:无法在 ReactJS 中读取未定义的属性(读取“道具”)

ReactJS失败,并显示错误“无法读取未定义的属性'map'”

类型错误:无法从未定义中读取属性“ x”。仅当我包含值数组时

角度:错误类型错误:数据正确显示时,无法读取属性'choice_set'为null

无法读取Reactjs中undefined的属性

在 ReactJs 中使用 map 函数时出现错误“无法读取未定义的属性‘map’”

类型错误:按任意键时无法读取 null 的属性“名称”react reactjs javascript redux

尝试复制时粘贴输入字段的值:无法读取未定义的属性“ nodeType”

Angular 6-错误TypeError:显示输出时,无法读取未定义的属性“ firstName”

类型错误:无法读取未定义的属性“地图”-reactjs 中的购物车

类型错误:无法读取 ReactJS 表单字段中未定义的属性“名称”

类型错误:无法读取reactjs中未定义的属性“ map”

未捕获的类型错误:无法读取 reactjs 中未定义的属性“类函数”

类型错误:无法读取 ReactJS Jest 测试中未定义的属性“标题”

类型错误:无法在打字稿中读取未定义、reactjs 的属性“map”

orderby无法正常工作。显示错误(TypeError:无法读取[null]中未定义的属性“ toLowerCase”)

无法从输入 reactjs 中删除值字符

无法在 reactJs 中读取 null 的属性(读取“长度”)

React JS 错误,处理登录表单时无法读取 null 的属性“值”

类型错误:使用 Redux 向数组添加值时,无法读取未定义的属性“值”