javascript - React Native Fetch API not returning my calls - Stack Overflow

Sorry for the joke in the title.I am currently exploring the fetch API in react native, but I have bump

Sorry for the joke in the title.

I am currently exploring the fetch API in react native, but I have bumped in to some issues which I cannot wrap my head around.

So, I am trying to get a message from a server, which I am calling with the fetch API in the following manner:

var serverCommunicator = {
    test: function() {
        fetch(baseUrl  , {
          method: 'GET',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          }
        })
        .then((response) => response.text())
        .then((responseText) => {
             return (JSON.stringify(responseText));
        })
        .catch((error) => {
          console.warn(error);
        }).done();
    },
module.exports = serverCommunicator;

When I tested using only console.log(responseText) my log gave me the correct message. However, now when I wanna try to put the content in the code as a message in a View it does not return as expected. Calling it in the following manner:

import Method from '../Services/Methods';
.
.
.
<View style={styles.card}>
   <CardView
      message={Method.test()} 
    />

I can see how the function test is called properly when calling it like this, but for some reason, it does not write the message.

Sorry for the joke in the title.

I am currently exploring the fetch API in react native, but I have bumped in to some issues which I cannot wrap my head around.

So, I am trying to get a message from a server, which I am calling with the fetch API in the following manner:

var serverCommunicator = {
    test: function() {
        fetch(baseUrl  , {
          method: 'GET',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          }
        })
        .then((response) => response.text())
        .then((responseText) => {
             return (JSON.stringify(responseText));
        })
        .catch((error) => {
          console.warn(error);
        }).done();
    },
module.exports = serverCommunicator;

When I tested using only console.log(responseText) my log gave me the correct message. However, now when I wanna try to put the content in the code as a message in a View it does not return as expected. Calling it in the following manner:

import Method from '../Services/Methods';
.
.
.
<View style={styles.card}>
   <CardView
      message={Method.test()} 
    />

I can see how the function test is called properly when calling it like this, but for some reason, it does not write the message.

Share Improve this question edited May 4, 2016 at 18:41 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked May 4, 2016 at 18:35 Niklas LindekeNiklas Lindeke 3901 gold badge7 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This is a classic async issue, where your then function returns after render has already been called, with nowhere to return to.

Most mon solution: display empty state message / loading indicator and fetch your server info when your ponent mounts. When your promise returns and then callback is fired, set the ponent state which will trigger a re-render, with your expected value.

class extends React Component

class YourComponent extends Component {
  constructor() {
    super()
    this.state.text = 'Loading, please wait!' // default text
  }

  ponentDidMount() {
    fetch(baseUrl, options)
      .then((response) => response.text())
      .then((responseText) => {
         this.setState({ text: responseText }) // this triggers a re-render!
      })
  }

  render() {
    return (
      <View style={styles.card}>
        <CardView
          message={this.state.text} // <-- will change when fetch call returns
        />
      </View>
    )
  }

}

React.createClass

var YourComponent = React.createClass({
  getInitialState() {
    return { text: 'Loading, please wait!' }
  }, // <-- ma between functions, because object keys

  ponentDidMount() {
    fetch(baseUrl, options)
      .then((response) => response.text())
      .then((responseText) => {
         this.setState({ text: responseText }) // this triggers a re-render!
      })
  },

  render() { /* ..same as above.. */ }
})

If you want to keep your current architecture where the fetch call is in the service, you need to return the initial call to fetch, which will return a promise. Then you can hook up then in your constructor:

var serverCommunicator = {
  test: function() {
    return fetch(baseUrl, options) // return a promise! ..important!
      .then((response) => response.text())
      .then((responseText) => {
         return responseText
      })
  }
}

Then your imported function will return a promise..

import Method from '../Services/Methods'

...

ponentDidMount() {
  Method.test().then(responseText => {
    this.setState({ text: responseText })
  })
]

  ....

Hope that clears things up a bit about how promises work, and how to capture async data using state in React!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744956209a4603206.html

相关推荐

  • javascript - React Native Fetch API not returning my calls - Stack Overflow

    Sorry for the joke in the title.I am currently exploring the fetch API in react native, but I have bump

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信