我尝试从Employee ID字段中获取值,并将其添加到JSON中的对象属性employee:{id:}中。JSON中的其他元素工作得很好,这是我无法理解的嵌套元素。
这是为员工提交工时记录卡的应用程序。后端如果是用java编写的。我已经编写并测试了API。现在只想用React编写一些UI代码。
import { Component } from "react";
import {Form, Row, Col, Button} from 'react-bootstrap';
import axios from 'axios';
class TimeCard extends Component {
constructor(props) {
super(props);
this.state = {
date : '',
in : '',
out : '',
**employee : {
id : ''**
}
}
}
handleChange = (e) => {
this.setState({[e.target.name] : e.target.value});}
async SubmitTime (e) {
console.log(this.state);
let response = axios.post('http://.../timecard/add', this.state);
console.log(response);
console.log("this is executed");
}
render () {
const {date, timein, out, **employee**} = this.state;
return (
<div>
<br></br>
<h2 className="display-6">Daily Timecard</h2>
<Form>
<br></br>
<Row>
<div className = 'col-xs-4'>
<Col>
<**Form.Control onChange = {this.handleChange.bind(this)} type = 'text' placeholder = 'Employee ID' name = 'id' value = {employee.id} />**
</Col>
</div>
</Row>
<br></br>
<Row>
<div className = 'col-xs-4'>
<Col>
<Form.Control onChange = {this.handleChange.bind(this)} type = 'date' name = 'date' value = {date} />
</Col>
</div>
</Row>
<br></br>
<Row>
<div className = 'col-xs-4'>
<Col>
Time In <Form.Control onChange = {this.handleChange.bind(this)} type = 'time' name = 'in' value = {timein} />
</Col>
</div>
<div className = 'col-xs-4'>
<Col>
Time Out <Form.Control onChange = {this.handleChange.bind(this)} type = 'time' name = 'out' value = {out}/>
</Col>
</div>
</Row>
<br></br>
<Row>
<div>
<Col>
<Button onClick = {this.SubmitTime.bind(this)} >Submit Timecard</Button>
</Col>
</div>
</Row>
</Form>
</div>
)
}
}
export default TimeCard;
发布于 2021-07-09 02:30:54
您可以从employee
中解构嵌套属性id
,如下所示。
const {date, timein, out, employee: { id } } = this.state;
https://stackoverflow.com/questions/68310256
复制相似问题