react-native网络请求fetch

矫情吗;* 2022-05-22 10:12 331阅读 0赞

学习交流:https://gitee.com/potato512/Learn_ReactNative

react-native学习交流QQ群:806870562

效果图

70

代码示例

  1. import React from 'react';
  2. import {View,Text,Button} from 'react-native';
  3. export default class NetworkRequest extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. //key : value
  8. message:""
  9. };
  10. this.datalist = []
  11. }
  12. componentDidMount() {
  13. console.log("componentDidMount");
  14. loadDataFetch(this)
  15. }
  16. render(){
  17. return(
  18. <View style={
  19. {marginTop:60}}>
  20. <Text style={
  21. {margin:10,height:40}}>网络请求功能</Text>
  22. <Button title='请求数据' onPress={() => {loadDataFetch(this)}} />
  23. <Text style={
  24. {margin:10,height:40}}>请求信息:{this.state.message}</Text>
  25. </View>
  26. )
  27. }
  28. }
  29. const loadDataFetch = (that) => {
  30. fetch('https://www.apiopen.top/satinApi?type=1&page=1').then(
  31. (response) => response.json()
  32. ).then(
  33. (responseJson) => {
  34. that.datalist = []
  35. console.log("responseJson:" + responseJson);
  36. that.datalist = that.datalist.concat(responseJson.data);
  37. console.log("datalist: " + that.datalist);
  38. console.log("datalist object: " + that.datalist[0]);
  39. for (key in that.datalist[0]){
  40. console.log("datalist object: key = " + key + ", value = " + that.datalist[0][key]);
  41. }
  42. that.setState({
  43. message:that.datalist[0]['text']
  44. })
  45. }
  46. ).catch(
  47. (error) => {
  48. console.log("错误:" + error);
  49. }
  50. );
  51. }

发表评论

表情:
评论列表 (有 0 条评论,331人围观)

还没有评论,来说两句吧...

相关阅读

    相关 fetch请求详解

    最新更新时间:2019年06月04日16:18:30 [`《猛戳-查看我的博客地图-总有你意想不到的惊喜》`][-_-] > 本文内容:fetch相关 概述