React Native - HTTP

React Native - HTTP 首页 / React Native入门教程 / React Native - HTTP

在本章中,我们将向您展示如何使用提取处理网络请求。

App.js

无涯教程网

import React from 'react';
import HttpExample from './http_example.js'

const App = () => {
   return (
      <HttpExample />
   )
}
export default App

使用Fetch

一旦安装了组件,我们将使用 componentDidMount 生命周期方法从服务器加载数据,此函数将向服务器发送GET请求,返回JSON数据,将输出记录到控制台并更新我们的状态。

http_example.js

链接:https://www.learnfk.comhttps://www.learnfk.com/react-native/react-native-http.html

来源:LearnFk无涯教程网

import React, { Component } from 'react'
import { View, Text } from 'react-native'

class HttpExample extends Component {
   state = {
      data: ''
   }
   componentDidMount = () => {
      fetch('https://www.learnfk.com/posts/1', {
         method: 'GET'
      })
      .then((response) => response.json())
      .then((responseJson) => {
         console.log(responseJson);
         this.setState({
            data: responseJson
         })
      })
      .catch((error) => {
         console.error(error);
      });
   }
   render() {
      return (
         <View>
            <Text>
               {this.state.data.body}
            </Text>
         </View>
      )
   }
}
export default HttpExample
React Native HTTP

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

数据分析实战45讲 -〔陈旸〕

大规模数据处理实战 -〔蔡元楠〕

小马哥讲Spring核心编程思想 -〔小马哥〕

人人都能学会的编程入门课 -〔胡光〕

Java业务开发常见错误100例 -〔朱晔〕

高楼的性能工程实战课 -〔高楼〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)