React: React集成脚本库Fetch
一、简介
React功能虽然很强大,但是说到底它仍然只是一个简单的创建视图的脚本库,如果想要实现一些更为复杂的业务逻辑,我们还需要使用React搭配其他的脚本库协同工作,以提高应用程序的性能。其中,Fetch就是一款优秀的polyfill请求脚本库,它允许用户使用Promise对象方便地创建API调用。例如Fetch脚本库中的isomorphic-fetch库。对于Fecth脚本库的更多详细介绍可以参考此博主huangpb0624的博文。
二、安装
使用npm进行安装即可,安装如下:
//方式一:安装fetch版本isomorphic-fetch
npm install isomorphic -fetch --save
import fetch from "isomorphic-fetch"; //方式二:安装 Fetch Polyfill的 whatwg-fetch脚本库,可以解决 fetch 的兼容性
npm install --save whatwg-fetch
import 'whatwg-fetch'
使用npm安装后,在项目使用yarn更新依赖库,结果如下所示:

三、使用
在前面我们介绍了组件的生命周期,这里也就派上用场了。组件的生命周期函数为开发者提供了一个地方专门集成JavaScript脚本库。在这种情况下,这个地方也是我们将会发起API调用的地方。组件发送API调用时必须处理延迟问题,等待响应的问题等等,当然此处我们可以通过更新state解决,示例如下:
import React, { Component } from 'react';
import fetch from "isomorphic-fetch";
export default class App extends Component {
constructor(props){
super(props);
this.state = {
countryNames:[],
loading:false
}
}
//请求国家列表
componentDidMount() {
this.setState({loading: true});
fetch('https://restcountries.eu/rest/v1/all')
.then(response => response.json()) //转成json
.then(json => json.map(country => country.name))
.then(countryNames => this.setState({countryNames, loading: false}))
}
render() {
const {countryNames, loading} = this.state;
console.log(countryNames);
return (
(loading)? <div> Loading Country Names ...</div> :
(!countryNames.length) ? <div>No country Names</div> :
<ul>
{countryNames.map(
(countryName,i) => <li key={i}>{`国家:${countryName}`}</li>
)}
</ul>
);
}
}
打印结果:
() ["Afghanistan", "Åland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "The Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "United States Minor Outlying Islands", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Republic of the Congo", "Democratic Republic of the Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Curaçao", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern and Antarctic Lands", "Gabon", "The Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See", …]
[ … ]
[ … ]
[ … ]
length:
__proto__: Array()
展示结果:

React: React集成脚本库Fetch的更多相关文章
- 将React Native集成至Android原生应用
将React Native集成至Android原生应用 Android Studio 2.1 Preview 4生成的空项目 react-native 环境 0.22.2 初次编译后apk有1.1M, ...
- Angular团队公布路线图,并演示怎样与React Native集成
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/06/angular-2-react-native-roadmap 前不久在旧 ...
- [RN] React Native 使用开源库 react-native-image-crop-picker 实现图片选择、图片剪裁
React Native 使用开源库 react-native-image-crop-picker 实现图片选择.图片剪裁 该库可以实现启动本地相册和照相机来采集图片,并且提供多选.图片裁剪等功能,支 ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!
各位哥哥姐姐弟弟妹妹小伙伴们春节好! 2014年对于我们程序员很重要, 因为今年是 "码" 年! SS在此重要之年到来之际热力推出两大重要功能恭贺新春: 1. 浏览器视图 以前SS ...
- [React] React Fundamentals: Integrating Components with D3 and AngularJS
Since React is only interested in the V (view) of MVC, it plays well with other toolkits and framewo ...
- Android中集成第三方库的方法和问题
Android中集成第三方库的方法和问题 声明: 1. 本文參考了网上同学们的现有成果,在此表示感谢,參考资料在文后有链接. 2. 本文的重点在第三部分,是在开发中遇到的问题及解决的方法.第一,第二部 ...
- 【翻译】在Ext JS集成第三方库
原文地址:http://www.sencha.com/blog/integrating-ext-js-with-3rd-party-libraries/ 作者:Kevin Kazmierczak Ke ...
随机推荐
- 构建调试Linux内核网络代码的环境MenuOS系统
构建MenuOS系统 1.将指定文件拷贝到本地: git clone https://github.com/mengning/linuxnet.git 此过程可能需要输入github账号和密码. 2. ...
- Mysql多表关系
mysql多表关系 多表关系是关系型数据库特有的 三种关系 一对一关系 一对多关系 多对多关系 总结 一对一 例子:用户和用户信息 外键设置在用户上,外键字段唯一非空 添加 无级联:先增加被关联表记录 ...
- CSS 导入-选择器-权重
CSS 导入-选择器 Cascading Style Sheets 层叠样式表 它用来控制网页样式,并允许将样式代码与网页内容分离的一种标记性语言 CSS语法结构 选择器 声明{} 属性名:属性值 c ...
- php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理
6月13日任务 12.21 php-fpm的pool12.22 php-fpm慢执行日志12.23 open_basedir12.24 php-fpm进程管理 12.21 php-fpm的pool p ...
- xshell使用xftp传输文件、使用pure-ftpd搭建ftp服务
6月25日任务 15.4 xshell使用xftp传输文件15.5 使用pure-ftpd搭建ftp服务扩展vsftp使用mysql存放虚拟用户并验证 http://www.aminglinux.co ...
- 【读一本书】《昇腾AI处理器架构与编程》--神经网络基础知识(2)
1 卷积神经网络:输入层 之前提到多层感知机的参数太多,导致训练耗时长并且对图像处理也不具有优势,因此大神们 就提出了多层神经网络,其中最经典的是卷积神经网络(Convolution Neural N ...
- Python装饰器总结,带你几步跨越此坑!
欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...
- EventSource 实时传输数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML5基础 实例
<!DOCTYPE html><html> <head> <title>李清照简介</title> </head> <bo ...
- FlyWay工作原理
本文译自Flyway官方文档,原文地址https://flywaydb.org/getstarted/how 当你最开始将FlyWay指向一个空数据库时. 它会试着去查找schema历史表,如果此时数 ...