未捕获的TypeError:无法读取属性' enqueueSetState'未定义的

时间:2018-05-08 12:28:43

标签: c# asp.net reactjs typescript core

我试图用一个参数来调用这个函数来映射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);

2 个答案:

答案 0 :(得分:0)

而不是

this.setState({ filterCategories: response })

只是做

this

由于您正在使用箭头功能,#Check to see if two lines intersect # return a 1 if they intersect # return a 0 if the do not intersect def line_intersection_test( p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x,p3_y): s1_x = float(p1_x - p0_x) s1_y = float(p1_y - p0_y) s2_x = float(p3_x - p2_x) s2_y = float(p3_y - p2_y) outcome = 0 s = float (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y) t = float ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y) if (s >= 0 and s <= 1 and t >= 0 and t <= 1): outcome = 1 return outcome #basically feed line segment and the three lines that form the triangle separately to line_intersect_test to see if they intersect def triangle_intersection_test(vecX1, vecY1, vecX2, vecY2, triX1, triY1, triX2, triY2,triX3, triY3): side_1_test = line_intersection_test(vecX1, vecY1, vecX2, vecY2, triX1, triY1, triX2, triY2) side_2_test = line_intersection_test(vecX1, vecY1, vecX2, vecY2, triX2, triY2, triX3, triY3) side_3_test = line_intersection_test(vecX1, vecY1, vecX2, vecY2, triX1, triY1, triX3, triY3) result = side_1_test + side_2_test + side_3_test if result > 0: outcome = "motion detected" else: outcome = "motion not detected" return outcome 仍然是正确的。

答案 1 :(得分:0)

之所以未定义this是因为,如果您在ajax成功方法this中调用this是指响应的范围。

尝试在ajax请求上方执行类似const that = this;的操作。然后在成功方法中调用that.setState(...)。在这种情况下,that是指组件的当前实例。

我知道这是一个肮脏的解决方法,但是它可以工作。