"Uncaught (in promise) TypeError: _this.setState is not a function"错误



每次用户滚动经过航路点时,我都会尝试加载新数据。然而,当axios试图获取数据时,我得到了这个错误,尽管我已经浏览了很多帖子,但我不知道哪里错了。

Uncaught (in promise) TypeError: _this.setState is not a function    

下面是我的代码,谢谢你的建议。

import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
import { Waypoint } from 'react-waypoint';

export class PostIndex extends React.Component {
constructor(props) {
super(props);
this.state = {
page: 1,
posts: [],
};
}
componentDidMount() {
this.getPosts();
}
getPosts = () => {
axios.get('/api/posts', {params: {page: this.state.page}})
.then(response => { 
this.setState({
posts: response.data.posts
});
})
this.setState = ({ page: this.state.page += 1 });
}

render () {
return (
<div>
<p> i am post index </p>
<section>
<div style={{height: 1500}} />
<Waypoint
onEnter={this.getPosts}
/>
</section>
</div>
);
}
}
export default PostIndex;

更改此

this.setState = ({ page: this.state.page += 1 });

进入

this.setState({ page: this.state.page += 1 });

最新更新