May 6, 2022
6
Level 1
how to get single post data from wordpress rest api to react using axios
I'm using the following to fetch a single post from the WordPress REST API.
import React, { Component } from 'react';
import axios from 'axios';
class Pages extends Component {
constructor() {
super();
this.state = {
post: [],
};
}
componentDidMount() {
axios.get('http://localhost:8080/tapline/index.php/wp-json/wp/v2/pages/2')
.then(response => {
this.setState({ post: response.data });
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<div>
{this.state.post.map(single => {
return(
<div>
<h1 >{single.title.rendered}</h1>
</div>
);
})}
</div>
);
}
}
export default Pages;
Is there a better/more straightforward way to render the post without mapping an array? or how to render single post data
Please or to participate in this conversation.