简体   繁体   English

在单击屏幕之后,React Native Fetch不会呈现响应

[英]React Native Fetch does not render response until after clicking screen

So I've been building an app that uses the Fetch API to make a request. 所以我一直在构建一个使用Fetch API发出请求的应用程序。 Unfortunately, I think the code shown in the Facebook docs are (may be?) incorrect. 不幸的是,我认为Facebook文档中显示的代码(可能是?)不正确。

Problem 问题

When starting the app, Fetch makes a call to a URL to pull JSON data. 启动应用程序时,Fetch会调用URL来提取JSON数据。

componentDidMount(){
 return fetch(REQUEST_URL)
  .then((response) => response.json())
  .then((responseJson) => {
    this.setState({
      isLoading: false,
      data: JSON.stringify(responseJson)
    })
  })
  .catch((error) => {
    console.error(error);
  });
}

This is similar to the code that Facebook docs give as an example. 这类似于Facebook文档提供的代码。

The problem is that when I start the app, the data doesn't render in the render function. 问题是,当我启动应用程序时,数据不会在渲染功能中呈现。

render() {

 if (this.state.isLoading) {
   return (
    <View style={{flex: 1, paddingTop: 20}}>
      <ActivityIndicator />
    </View>
  );
 } 

var data = this.state.data;
return this.renderData(data); 
}

At least, it doesn't render until after I click/touch the screen. 至少,直到我点击/触摸屏幕才会渲染。 Once I touch the screen, the data renders. 触摸屏幕后,数据呈现。 Otherwise, it just stays in a perpetual "loading" state. 否则,它只会处于永久的“加载”状态。

Solution? 解?

I remembered that sometime in the past, I had to bind event handlers to their respective controls (this.handleClick = this.handleClick.bind(this), for example) 我记得在过去的某个时候,我不得不将事件处理程序绑定到各自的控件(例如,this.handleClick = this.handleClick.bind(this))

And that got me thinking: Where is this in the promise request? 这让我想到:承诺请求中的这个在哪里? Where does this point to? 指向哪里?

componentDidMount(){
 return fetch(REQUEST_URL)
  .then((response) => response.json())
  .then((responseJson) => {
    this.setState({ <--- **Where are you going?**
      isLoading: false,
      data: JSON.stringify(responseJson)
    })
  })

I console.logged "this" within the responseJson promise, and it does point to the component, but I'm not convinced. 我在responseJson的promise中记录了“this”,它确实指向了组件,但我不相信。 With it being buried inside of the promise, I don't think the this.setState function is actually able to set the component's State. 由于它被置于promise的内部,我认为this.setState函数实际上不能设置组件的State。

So I made a variable before the fetch request. 所以我在获取请求之前创建了一个变量。

const that = this;
return fetch(REQUEST_URL)

.then((response) => response.json())
.then((responseJson) => {

  that.setState({

    isLoading: false,
    data: JSON.stringify(responseJson)
  })
})

Now I'm not a software guru. 现在我不是软件大师。 I've been teaching myself this stuff for the last couple of years, so my logic is off half the time. 在过去的几年里,我一直在教自己这些东西,所以我的逻辑关闭了一半。

So if my reasoning is correct or incorrect, please let me know! 所以,如果我的推理是正确的或不正确的,请告诉我! What I do know is that this is working for me; 我所知道的是,这对我有用; once the data is fetched, it automatically sets the state and reloads, showing me the JSON data when rendered. 获取数据后,它会自动设置状态并重新加载,在呈现时显示JSON数据。

Any thoughts? 有什么想法吗? Am I completely off here? 我完全离开了吗? Am I missing anything? 我错过了什么吗?

I saw a problem like yours. 我看到了像你这样的问题。 It's not about your code. 这不是关于你的代码。 Are using the app in the Remote JS debugging mode? Remote JS debugging模式下使用该应用程序? If so, disable it. 如果是这样,请将其禁用。 If it didn't work, try to install a release version. 如果它不起作用,请尝试安装发行版。

(Posted on behalf of the OP) . (代表OP发布)

Apparently it was due to running the app in debug mode. 显然这是由于在调试模式下运行应用程序。 Running the app with this.setState works just fine when not in debug mode. 使用this.setState运行应用程序在不处于调试模式时工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM