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

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

技术教程推荐

说透中台 -〔王健〕

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

正则表达式入门课 -〔涂伟忠〕

分布式系统案例课 -〔杨波〕

Spark核心原理与实战 -〔王磊〕

体验设计案例课 -〔炒炒〕

基于人因的用户体验设计课 -〔刘石〕

程序员的个人财富课 -〔王喆〕

AI大模型系统实战 -〔Tyler〕

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