Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在react native中每秒自动加载数据?_Javascript_React Native - Fatal编程技术网

Javascript 如何在react native中每秒自动加载数据?

Javascript 如何在react native中每秒自动加载数据?,javascript,react-native,Javascript,React Native,我想每秒钟自动加载一次数据 我使用了setinterval,但它总是说这样的错误 “类型错误:未定义不是函数(靠近“…this.state.forumContentData.map…”)” 以下是我尝试过的: constructor(props) { super(props); this.state = { forumContentData: [] } } componentDidMount() { // Set timer before going to the Ne

我想每秒钟自动加载一次数据

我使用了setinterval,但它总是说这样的错误

“类型错误:未定义不是函数(靠近“…this.state.forumContentData.map…”)”

以下是我尝试过的:

constructor(props) {
  super(props);
  this.state = {
    forumContentData: []
  }
}

componentDidMount() {
  // Set timer before going to the Next Action - Every Seconds
  this._interval = setInterval(() => {
    this.getForumContent();
  }, 1000);
}

componentWillUnmount() {
    clearInterval(this._interval);
}

getForumContent() {
  return fetch('http://' + ipAddress() + ':' + portAddress() + '/api/forum/view_thread', {
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
  })
  .then((response) => response.json())
  .then((responseJson) => {
    this.setState({forumContentData: responseJson}); // Get the data from API
  })
  .catch((error) => {
    //console.error(error);

    alert(error);
  });
}

render() {

  let forumContentResult = this.state.forumContentData.map((forumContentDataDetail, index) => {
      var forum_title = forumContentDataDetail.title;

      return (
        <Text>{forum_title}</Text>
      );
  });


}
构造函数(道具){
超级(道具);
此.state={
forumContentData:[]
}
}
componentDidMount(){
//在进行下一个操作之前设置计时器-每秒钟一次
这个。_interval=setInterval(()=>{
这是.getForumContent();
}, 1000);
}
组件将卸载(){
clearInterval(此._interval);
}
getForumContent(){
返回fetch('http://'+ipAddress()+':'+portAddress()+'/api/forum/view_thread'{
方法:“GET”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
}
})
.then((response)=>response.json())
.然后((responseJson)=>{
this.setState({forumContentData:responseJson});//从API获取数据
})
.catch((错误)=>{
//控制台错误(error);
警报(错误);
});
}
render(){
让forumContentResult=this.state.forumContentData.map((forumContentDataDetail,index)=>{
var forum_title=forumContentDataDetail.title;
返回(
{论坛名称}
);
});
}

在react native中每秒钟自动加载数据的最佳方式是什么?

在setInterval中,上下文是不同的。您绑定的是setInterval,而不是来自承诺的响应

试试这个:

getForumContent() {
let self = this;
  return fetch('http://' + ipAddress() + ':' + portAddress() + '/api/forum/view_thread', {
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
  })
  .then((response) => response.json())
  .then((responseJson) => {
    self.setState({forumContentData: responseJson}); // Get the data from API
  })
  .catch((error) => {
    //console.error(error);

    alert(error);
  });
}

我猜你的问题在这里。react渲染一次并尝试获取
{form_title}
,但它不在那里,因为您需要等待AJAX的返回。尝试用类似于
if(AJAX.success){return(//您的代码):}或者{return(Loading…)}
的方式包装渲染返回,即使我已经添加了条件,错误仍然会显示出来。问题是我在间隔中设置的请求无法停止。实际上我已经添加了clear interval
clear interval(这个.\u interval)但它仍在加载数据。错误仍会显示。即使我已移动到另一个组件,它仍会继续加载数据。实际上,我已经清除了间隔,但它仍然继续加载数据<代码>清除间隔(此间隔)我不认为是间隔。什么是responseJson值?此外,您处理map函数的方式也不正确。如果您在响应中有多个项目,那么您实际上是在进行多次返回。