【问题标题】:JsonArray Listview with React Native?带有 React Native 的 JsonArray Listview?
【发布时间】:2017-03-16 14:30:01
【问题描述】:

这可能是一个愚蠢的问题,我试图从服务器获取 json 数组并将其设置在 listview 中,今天我才开始做 react js,所以有点困惑如何做到这一点得到 null 不是一个对象(评估 'this.state. datasource') 不知道在哪里出错让我发布我的代码到目前为止我尝试过的是 index.android 代码:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  ListView,
  View
} from 'react-native';

export default class SecondProject extends Component {
      constructor(props) {
    super(props);
    this.dataSource = new ListView.DataSource({
    rowHasChanged: (r1, r2) => r1 !== r2
  })
  }
  componentDidMount () {
   this.getContractList()
}

  render() {
    this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseJson)});
    return (
      <View style={{flex: 1, paddingTop: 22}}>
        <ListView
          dataSource={rows}
          renderRow={(rowData) => <Text>{rowData}</Text>}
        />
      </View>
    );
  }
    getContractList() {
      return fetch('url')
        .then((response) => response.json())
        .then((responseJson) => {
          this.setState({isLoading: false, jsonData: responseJson});
        ///  console.log(responseJson);
          return responseJson;
        })
        .catch((error) => {
         // console.error(error);
        });
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('SecondProject', () => SecondProject); 

这是来自服务器的 json 响应:

[
        {
        "HeaderText":"Contract Approval",
        "ApprovalType":"C",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        },
        {
        "HeaderText":"Contract Specification Approval",
        "ApprovalType":"CS",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        },
        {
        "HeaderText":"Spare Part Request Approval",
        "ApprovalType":"SPR",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        },
        {
        "HeaderText":"Spare Part Non Return Approval",
        "ApprovalType":"SPNR",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        },
        {
        "HeaderText":"Spare Purchase Request Approval",
        "ApprovalType":"SPRA",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        },
        {
        "HeaderText":"Spare Request Approval",
        "ApprovalType":"SSRA",
        "ApprovalCount":0,
        "SubText":"New / Change",
        "ApproverID":198
        }
]

如何处理这个问题,有人可以帮助我提前谢谢!

【问题讨论】:

  • 你试过 { this.state.datasource && {rowData}} />}
  • 无法添加整个项目

标签: android json reactjs react-native


【解决方案1】:

试试这样的 -

constructor(props) {
  super(props);

  this.state = {
    jsonData: null
  };

  this.dataSource = new ListView.DataSource({
    rowHasChanged: (r1, r2) => r1 !== r2
  });
}

  render() {
    return (
      <View style={{flex: 1, paddingTop: 22}}>
        {this.state.jsonData &&
          <ListView
            dataSource={this.dataSource.cloneWithRows(this.state.jsonData)}
            renderRow={(rowData) => <Text>{rowData.title}</Text>}
          />}
      </View>
    );
  }

  getContractList() {
    var url = 'https://facebook.github.io/react-native/movies.json';
    fetch(url)
      .then((response) => response.json())
      .then((responseJson) => {
        console.log(responseJson);
        this.setState({isLoading: false, jsonData: responseJson.movies});
      })
      .catch((error) => {
       //console.error(error);
      });
  }

【讨论】:

  • 它应该可以工作...检查我使用示例 json 的更新答案
  • 你错过了 componentDidMount 我已经添加它现在可以使用了,谢谢
  • 确定我如何在 json 的 listview 中添加多个项目
【解决方案2】:

我不是 react native 方面的专家,但我注意到在您的 fetch('url')中,URL 没有在任何地方定义。我建议将 Json 的 URL 放在那里。

【讨论】:

  • 我刚刚提到了 url 而不是我的 ip
猜你喜欢
  • 2017-06-24
  • 2018-04-24
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 2018-02-05
相关资源
最近更新 更多