Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
在React Native中将数据从AJAX调用传递到视图_Ajax_React Native - Fatal编程技术网

在React Native中将数据从AJAX调用传递到视图

在React Native中将数据从AJAX调用传递到视图,ajax,react-native,Ajax,React Native,我是新手,但对本机iOS开发了解很多。我正试着把我的头脑集中在技术上;我为我的无知提前道歉。请尽量帮助别人,不要伤害别人。因此,我有一个登录页面,可以将数据传递到下一个视图。代码如下: _getHours = () => { this.setState({ isLoading: true }); fetch('https://seniordevops.com/clockin/list', { method: 'GET', headers:

我是新手,但对本机iOS开发了解很多。我正试着把我的头脑集中在技术上;我为我的无知提前道歉。请尽量帮助别人,不要伤害别人。因此,我有一个登录页面,可以将数据传递到下一个视图。代码如下:

  _getHours = () => {
    this.setState({ isLoading: true });
    fetch('https://seniordevops.com/clockin/list', {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
        credentials: 'include'
      })
      .then(response => response.json())
      .then((responseJson) => {
        this.setState({ 
          isLoading: false , 
        });
        this.props.navigator.push({
          title: 'Clock In',
          component: Clockin,
          passProps: {intervals: responseJson}
        });
      })
      .catch(error =>
        this.setState({
          isLoading: false,
          message: 'Something bad happened ' + error
      }));
  } 
下一个视图运行平稳,然后我进入一个部分,在该部分中,我必须根据类似的API调用更新FlatList,这次使用的参数除外,代码如下:

  _getHours = (currentProject) => {
    this.setState({currentProject: currentProject });
    fetch('https://seniordevops.com/clockin/list?project=' + currentProject, {
      method: 'GET',
      headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
      credentials: 'include',
    })
    .then(response => response.json())
    .then((responseJson) => {
      alert(JSON.stringify(responseJson));
    })
    .catch(error =>
      this.setState({
        isLoading: false,
        message: 'Something bad happened ' + error
    }));
  }
只是这一次,我不知道如何继续。我的警报正在返回我想要的JSON,我只是不知道如何处理它。这是我的公寓清单:

<FlatList
  data={this.props.intervals}
  keyExtractor={this._keyExtractor}
  renderItem={this._renderItem}
  ListHeaderComponent={this.renderHeader}
/>


我知道状态更适合于动态变量,但我最初传入了一个道具。也许我需要设置一个默认道具,然后传递状态idk。如何告诉平面列表我有新数据并重新渲染?旁白/奖金问题:我应该考虑重构到网络类在某个点,如果是的话,使用ReDux?这就是为什么?专注于我的主要问题,然后如果你能给我一个整体的网络策略,你可以给我加分。

阅读文档,再考虑一下,这是我的解决方案。将此函数添加到iOS等效的ViewController中,在React中我们称之为默认组件类(我想?):

在我看来,此函数类似于ViewDidLoad()的iOS版本。这是您应该进行API调用的地方,但是我的调用已经从以前的ViewController中进行了,所以我设置了作为初始值传递的道具。然后将我的组件更改为监视状态而不是道具

<FlatList
  data={this.state.hours}
  keyExtractor={this._keyExtractor}
  renderItem={this._renderItem}
  ListHeaderComponent={this.renderHeader}
/>

调用
setState
会触发重新加载。
props.interval
是您要更改的数据吗?我找到了它,将数据链接在一起,以便在componentDidMount()中设置状态变量,感谢您的贡献。当我在iOS中在ViewController之间执行我称之为“segue”的操作时,我通过一个prop传入JSON,这就是props.interval。我只是想不出下一步是什么,因为我真的需要组件有一个状态。关键是在React文档中学习componentDidMount;这就解决了我的知识差距。关于何时使用Redux有什么提示吗?
<FlatList
  data={this.state.hours}
  keyExtractor={this._keyExtractor}
  renderItem={this._renderItem}
  ListHeaderComponent={this.renderHeader}
/>
.then((responseJson) => {
  this.setState({ hours: responseJson});
})