0

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 :)

4

1 回答 1

3

我只能为 Android 说话:如果您正在运行模拟器 localhost (127.0.0.1) 将指的10.0.2.2是您的实际 localhost。

所以在那种情况下你会使用这个: http://10.0.2.2:<port>/wordpress/wp-json/wp/v2/posts

希望这可以帮助

于 2017-08-10T06:39:31.920 回答