I am trying to fetch posts from wordpress RESTapi-v2 in react component.The api supplies data in json format.But fetch response in Reactjs is returning null array of posts
below is the code
class Posts extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
posts: ''
}
this.fetchposts = this.fetchposts.bind(this);
}
fetchposts(){
fetch("http://smashingdevs.com/wp-json/wp/v2/posts/")
.then(
(result)=> {
this.setState({
isLoaded: true,
posts: result,
});
}
);
console.log(this.state.posts);
}
render() {
return (
<div>
<h2>Hello there</h2>
<button onClick={this.fetchposts}>
fetch posts
</button>
</div>
);
}
}
this request requires no authentication you can check it form http://smashingdevs.com/wp-json/wp/v2/posts/.
How can i debug this? is there anyway to see what posts are being fetched in console?