c# - 未捕获的类型错误:无法读取未定义的属性“enqueueSetState”
问题描述
我正在尝试使用参数调用此函数以映射 filterCategories,但出现此错误。
interface FrontPageComponents {
filterCategories: GetTheActivity[];
}
export class FrontPage extends React.Component<RouteComponentProps<{}>, FrontPageComponents> {
constructor() {
super();
this.state = {filterCategories: []};
}
public sort(event: string) {
console.log(event);
return (
$.ajax({
type: "POST",
url: './Categ/GetFilterCategories',
dataType: 'json',
data: { _event: event },
success: (response) => {
FrontPage.prototype.setState({ //in this line the error
filterCategories: response
}) }
})
);
}
public render() {
return <div>
<Sorting />
</div>;
}
}
在排序组件中,我这样调用函数:
FrontPage.prototype.sort(event.target.innerText);
解决方案
之所以this
没有定义是因为如果this
在ajax里面调用success方法this
是指响应的范围。
尝试执行以下操作:const that = this;
在 ajax 请求之上。然后在success方法中调用that.setState(...)
。在这种情况下that
指的是组件的当前实例。
我知道这是一个肮脏的修复,但它有效。
推荐阅读
- javascript - 如果悬停在这个 div 上,不要运行脚本
- or-tools - google ortools vrp set break 失败
- python - 根据熊猫数据框中的其他列值组合列值
- c# - 如何在 C# 中使用 StreamReader 加载大文件?
- c# - VB.NET 中的 LINQ Any Query 是什么?
- angular - 我应该如何在 Angular 中连接这些对象?
- amazon-web-services - AWS Connect 的编程触发器 - 代理联系流
- php - Laravel - 计算多个时间条目的总时间
- firebase - 如何在 Firebase 云功能中获取经过验证的电话号码?
- javascript - react-router-dom: 无效的钩子调用,钩子只能在函数组件的主体内部调用