This is my very first app in React. I have created the component and when a user adds text to textArea and clicks on the button, "Download Pdf", I want to pass defaultValue to convertToPdf
function.
How do i do that? Basically, I am trying to create a PDF downloader. Any help will be appreciated.
import React, { Component } from "react";
import autosize from "autosize";
import Button from '@material-ui/core/Button';
export class PDFEditorComponent extends Component {
componentDidMount() {
this.textarea.focus();
autosize(this.textarea);
}
convertToPdf() {
this.setState(this.textarea);
console.log("TEXT", this.textarea);
}
render() {
const style = {
maxHeight: "175px",
minHeight: "450px",
minWidth: "800px",
resize: "none",
padding: "9px",
boxSizing: "border-box",
fontSize: "15px"
};
return (
<div>
PDF Downloader
<br />
<br />
<textarea
style={style}
ref={c => (this.textarea = c)}
placeholder="Paste pdf data"
rows={1}
defaultValue=""
/>
<br />
<Button
variant="contained"
color="primary"
onClick={() => this.convertToPdf(this.textarea)}
>
Download Pdf
</Button>
</div>
);
}
}
Bulletpoints:
constructor(props) {
super(props);
this.textareaRef = React.createRef();
}
then pass it to your textarea element like this
ref={this.textareaRef}
this.setState({value: this.textareaRef.current.value})
this.state = {
value: null;
}
and then whenever you want to change it (only from within this component), you call setState(), like I did in p. 2
Enjoy React, it's great:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.