why react setState is not updating state in class component in my code
In the console, my data is shown (console.log(data))
but setState is not updating state
whats the problem
export default class Posts extends Component {
constructor(props) {
super(props)
this.state = {
posts: null,
isPending: true
}
}
componentDidMount() {
fetch('http://localhost:5000/api/all_posts/')
.then(response => response.json())
.then(data => {
console.log(data)
this.setState({ posts: data });
})
}
render() {
return (
<div className="posts">
{this.isPending && <div>loading...</div>}
{this.state.posts && this.state.posts.map((post) => {
<div className="post" key={post.id}>
{post.slug}
</div>
})}
</div>
)
}
}