I'm using the create-react-app to build an React Native app that would show a list posts from WordPress. I have a problem fetching the data from WordPress. I think I'm not fully understanding how the fetch()
function works.
This is what I have at the moment:
export default class Posts extends Component {
constructor() {
super();
this.state = {
posts: []
}
}
componentDidMount() {
let dataURL = "http://localhost/wordpress/wp-json/wp/v2/posts";
fetch(dataURL)
.then(response => response.json())
.then(response => {
this.setState({
posts: response
})
})
}
render() {
let posts = this.state.posts.map((post, index) => {
return
<View key={index}>
<Text>Title: {post.title.rendered}</Text>
</View>
});
return (
<View>
<Text>List Of Posts</Text>
<Text>{posts}</Text>
</View>
)
}
}
This is the warning that I get:
Possible Unhandled Promise Rejection (id:0)
TypeError: Network request failed
Thanks in advanced for any help :)