[英]Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'state')
I tried to pass the state value using onSubmit function but it returns an Unhandled error which i couldn't find what the error is Kindly help me with this我尝试使用 onSubmit function 传递 state 值,但它返回一个未处理的错误,我找不到错误是什么请帮助我
React Code:反应代码:
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>
)
}
}
the above code is the table filter code if the state is null it returns all if it has value then it returns onFormSubmit function.上面的代码是表格过滤器代码,如果 state 是 null 它返回所有如果它有值然后它返回 onFormSubmit function。 I tried to a lot but couldn't find out since im new to react.
我尝试了很多,但由于我是新来的反应而无法找到。 i need this community to help me
我需要这个社区来帮助我
Also new to JS.也是 JS 的新手。 So, please take below with caution.
因此,请谨慎使用以下内容。
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)
Would you please try following steps:请您尝试以下步骤:
Why?为什么? The error, as it reads:
错误如下:
"... Cannot read properties of undefined reading ('state')"
“...无法读取未定义读取的属性('state')”
state
state
is expected to be a property, but that object is simply not defined.预计是一个属性,但 object 根本没有定义。
If I were you, I will try to add below line如果我是你,我会尝试在下面添加
this.onFormSubmit = this.onFormSubmit.bind(this)
just below略低于
this.handleChange = this.handleChange.bind(this);
on the constructor method.关于构造方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.