【发布时间】:2021-06-01 11:06:12
【问题描述】:
我想问我如何将输出变量(我通过 Axios 的 post 调用获得)附加到 textarea,它以输入变量作为其值。
我真的不知道如何做这样的事情......
我想得到下面视频https://www.youtube.com/watch?v=OcyBYfK6gfY中显示的相同结果
import React from "react";
import { Form, Button } from "react-bootstrap";
import axios from "axios";
import "bootstrap/dist/css/bootstrap.min.css";
const TRANSLATE_API_URL = "/translate";
class Interface extends React.Component {
constructor(props) {
super(props);
this.state = {
input: "",
output: "",
buttonText: "Submit",
description: "Description",
};
// Bind the event handlers
this.handleInputChange = this.handleInputChange.bind(this);
this.handleClick = this.handleClick.bind(this);
}
handleInputChange(e) {
this.setState({ input: e.target.value });
}
handleClick(e) {
e.preventDefault();
let body = {
prompt: this.state.input
};
axios.post(TRANSLATE_API_URL, body).then(({ data: { text } }) => {
this.setState({ output: text });
});
}
render() {
return (
<div>
<head />
<body style={{ alignItems: "center", justifyContent: "center" }}>
<div
style={{
margin: "auto",
marginTop: "80px",
display: "block",
maxWidth: "500px",
minWidth: "200px",
width: "50%"
}}
>
<Form onSubmit={this.handleClick}>
<Form.Group controlId="formBasicEmail">
<Form.Label>{this.state.description}</Form.Label>
<Form.Control
type="text"
as="textarea"
className="tarea"
placeholder="Enter text"
value={this.state.input}
onChange={this.handleInputChange}
></Form.Control>
</Form.Group>
<Button variant="primary" type="submit">
{this.state.buttonText}
</Button>
</Form>
{this.state.output}
</div>
</body>
</div>
);
}
}
export default Interface;
【问题讨论】:
标签: reactjs forms class append textarea