1

Hello i recently just started learning react native. I am now trying to fetch data from a wordpress api with the following code with this exact api address but nothing loads up.

    import React from 'react';
    import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

    export default class FetchExample extends React.Component {

    constructor() {
    super();
    this.state = {
      posts: []
    }
    }
    componentDidMount() {
    let dataURL = "https://click9ja.com/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>
     )
  }
}

Any advice will be appreciated.

2
  • What error message is shown in the browser devtools console? Commented Aug 29, 2018 at 7:56
  • I am using expo and there's no error at all. Commented Aug 29, 2018 at 9:03

2 Answers 2

1

Firstly wrap return value in parenthesis

let posts = this.state.posts.map((post, index) => {
      return (
      <View key={index}>
      <Text>Title: {post.title.rendered}</Text>
      </View> 
     );
    });

Secondly you cannot render variables in text tags. It should be something like this

<View>{varaible}</View>

Therefore, <Text>{posts}</Text> should just be {post}

Sign up to request clarification or add additional context in comments.

Comments

0

Fetch will have GET method as default when requesting to server. To set it to make a POST method or other methods you will need to add extra parameter in the fetch In your case it will be

fetch('https://click9ja.com/wp-json/wp/v2/posts', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'yourValue',
    secondParam: 'yourOtherValue',
  }),
});

Some API will need headers and body, please make sure to check the API Documentation before using it for understanding purpose.

For more information on Fetch in React native you can visit it here For more information on What is API you can visit it here

Hope this help.

6 Comments

Yes i understand the Fetch vs Post argument. I am trying to GET data from the API so FETCH should work for me right now, what i don't understand is why the code above isn't fetching data from that particular API.
what do you mean?
I have a wordpress API with a lot of data, the code above can't FETCH or GET the data. Do you understand me now?
try to send the request through Postman and see if it is fetch that is unable or the API that does not return anything.
@sir-kosawa It works on postman, it also work when I visit the API directly on web browser. I also substituted my API with the API from react native documentation on networking it still doesn't work. I'm tired.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.