【发布时间】:2021-12-27 16:06:45
【问题描述】:
我尝试使用 onSubmit 函数传递状态值,但它返回一个未处理的错误,我找不到错误是什么请帮我解决这个问题
反应代码:
import React, { Component } from 'react'
import 'bootstrap/dist/css/bootstrap.css';
import { DateRangePicker } from 'rsuite';
import moment from 'moment'
export default class DateComponent extends Component {
constructor() {
super();
this.state = {
datefilter: [],
startDate: "",
endDate: ""
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(start) {
console.log()
this.setState({
startDate: start[0],
endDate: start[1]
})
}
async componentDidMount() {
if (this.state.startDate == '') {
console.log(Object.keys(moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss')).length)
const response = await fetch('http://localhost:8081/items');
const data = await response.json();
this.setState({ datefilter: data })
}
}
async onFormSubmit() {
var start = moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss');
var end = moment(this.state.endDate).format('yyyy-MM-DDTHH:mm:ss');
console.log(Object.keys(moment(this.state.startDate).format('yyyy-MM-DDTHH:mm:ss')).length)
console.log(start,end)
var startDate = new Date(start).getTime();
var endDate = new Date(end).getTime();
const response = await fetch('http://localhost:8081/items');
const data = await response.json();
var result = data.filter(d => {
var time = new Date(d.Date).getTime();
return (startDate < time && time < endDate);
});
console.log(result);
this.setState({ datefilter: result })
}
render() {
const datefilter = this.state.datefilter
return (
<div>
<form onSubmit={this.onFormSubmit}>
<DateRangePicker
format="yyyy-MM-dd HH:mm:ss"
onChange={this.handleChange}
onOk={this.onFormSubmit}
placeholder="Enter Date and Time"
/>
</form>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Item ID</th>
<th scope="col">Item Name</th>
<th scope="col">Status</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
{datefilter.map(table => (
<tr>
<td>{table.ID}</td>
<td>{table.ItemName}</td>
<td>{table.Status}</td>
<td>{table.Date}</td>
</tr>
))}
</tbody>
</table>
</div>
)
}
}
上面的代码是表格过滤代码,如果状态为空则返回所有,如果有值则返回onFormSubmit函数。我尝试了很多,但由于我是新来的反应而无法找到。我需要这个社区来帮助我
【问题讨论】:
标签: javascript arrays reactjs unhandled-exception