我在使用React时调用函数中的函数时,在维护范围方面遇到了一些困难,我创建了一个onChange函数,它调用另一个名为buildBookingQuery 的函数
handlePerPageChange = (data) => {
const elementsIndex = this.state.filters.findIndex(element => element.id == 1)
let newArray = [...this.state.filters]
newArray[elementsIndex] = {...newArray[elementsIndex], perPage: parseInt(data.target.value)}
this.setState({
filters: newArray
})
console.log(this.state.filters[0])
console.log(newArray)
let query = this.buildBookingQuery() => This is the problem function
console.log(query)
}
我的buildBookingQuery函数
buildBookingQuery (){
let page = 1
let orderby = this.state.filters[0].orderby.split(" ")
let from = this.state.filters[0].fromDate
let to = this.state.filters[0].toDate
let reservedFrom = this.state.filters[0].reservedFromDate
let reservedTo = this.state.filters[0].reservedToDate
console.log("Build Booking Query:", from) => getting undefined
// rest of code removed for sake of brevity
return someValue
}
经过研究,我已经将绑定事件添加到构造函数中
this.buildBookingQuery = this.buildBookingQuery.bind(this);
但无济于事。
非常感谢您的帮助
将buildBookingQuery
函数更改为箭头函数,并删除绑定。
我的问题是我自己这行代码:
this.state.filters[0].fromDate
应该是:
this.state.filters[0].startDate
感谢你的帮助,漫长的一天和疲惫的眼睛!