[React Native] Error Handling and ActivityIndicatorIOS
With React Native you use ActivityIndicatorIOS to show or hide a spinner. In this lesson we combine ActivityIndicatorIOS with our HTTP requests in order to give the user feedback about data loading.
What you want is when calling the fetch api, showing the loading spinner. If success or not found user will hide the loadin spinner. For user not found, will show the error message.
import React, { Component, PropTypes } from 'react';
import { View, Text, StyleSheet, TextInput, TouchableHighlight, ActivityIndicatorIOS } from 'react-native';
import api from '../utils/api';
import Dashboard from './Dashboard';
var style = StyleSheet.create({
    mainContainer: {
        flex: ,
        padding: ,
        marginTop: ,
        flexDirection: 'column',
        justifyContent: 'center',
        backgroundColor: '#48BBEC'
    },
    title: {
        marginBottom: ,
        fontSize: ,
        textAlign: 'center',
        color: '#fff'
    },
    searchInput: {
        height: ,
        padding: ,
        marginRight: ,
        fontSize: ,
        borderWidth: ,
        borderColor: 'white',
        borderRadius: ,
        color: 'white'
    },
    buttonText: {
        fontSize: ,
        color: '#111',
        alignSelf: 'center'
    },
    button: {
        height: ,
        flexDirection: 'row',
        backgroundColor:'#8fefcc',
        borderColor:'white',
        borderWidth:,
        borderRadius:,
        marginBottom:,
        alignSelf:'stretch',
        justifyContent:'center'
    }
});
export default class Main extends Component{
    constructor(props){
        super(props)
        this.state = {
          username: '',
          isLoading: false,
          error: false
        };
    }
    handleChange(event){
        this.setState({
            username: event.nativeEvent.text
        })
    }
    handleSubmit(event){
        //update our indicatorIOS spinner
        this.setState({
            isLoading: true
        });
        //fetch data from github
        api.getBio(this.state.username)
            .then( (res) => {
                if(res.message === "Not Found"){
                    this.setState({
                        error: 'User not found',
                        isLoading: false
                    })
                }else{
                    //Pass in a new router component
                    this.props.navigator.push({
                        title: res.name || 'Selet an Option',
                        component: Dashboard,
                        passProps: {userInfo: res}
                    });
                    //Clean the search input and loading
                    this.setState({
                        isLoading: false,
                        error: false,
                        username: 'User not found'
                    });
                }
            })
    }
    render(){
        const showError = (
            this.state.error ? <Text>User not found</Text>: <View></View>
                    );
       return (
           <View style={style.mainContainer}>
               <Text style={style.title}>Search for a Github User</Text>
               <TextInput
                 style={style.searchInput}
                 value={this.state.username}
                 onChange={this.handleChange.bind(this)}
               />
               <TouchableHighlight
                style={style.button}
                onPress={this.handleSubmit.bind(this)}
                underlayColor="white"
               >
                   <Text style={style.buttonText}>SEARCH USER</Text>
               </TouchableHighlight>
               <ActivityIndicatorIOS
                animating={this.state.isLoading}
                size="large"
                color="#111"
               />
               {showError}
           </View>
       )
    }
}
[React Native] Error Handling and ActivityIndicatorIOS的更多相关文章
- [react native] Error loading page
		如上图显示的错误,解决方法如下: 在react native ios项目的info.plist文件中,新增一个属性. 在Info.plist中添加NSAppTransportSecurity类型Dic ... 
- [RN] React Native :Error: Cannot find module 'asap/raw'
		今天在使用 react-native-dropdownmenus 的时候,安装没问题,但Link的时候 报: Error: Cannot find module 'asap/raw' 朋友们莫慌,一步 ... 
- 解决React Native:Error: Cannot find module 'asap/raw'
		本来想做个底部切换的tab的,安装完 npm i react-native-tab-navigator --save 后跑项目就报错了,如下图 和我一样报这个错的朋友们莫慌,一步就可以解决了,执行命令 ... 
- React Native开发技术周报2
		(1).资讯 1.React Native 0.22_rc版本发布 添加了热自动重载功能 (2).技术文章 1.用 React Native 设计的第一个 iOS 应用 我们想为用户设计一款移动端的应 ... 
- React Native专题
		转载注明出处:地址:http://www.lcode.org本文出自:[江清清的技术专栏]本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶 ... 
- React Native专题-江清清
		本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 刚创建的React Native交流8群:533435865 欢迎各位大牛, ... 
- React Native 常用插件案例
		(二).基础入门: 1.React Native For Android环境配置以及第一个实例 2.React Native开发IDE安装及配置 3.React Native应用设备运行(Runnin ... 
- Flipper & React Native
		Flipper & React Native Flipper Flipper是一款用于调试移动应用程序的出色开发人员工具,在Android和iOS社区中颇受欢迎. Flipper is a g ... 
- react native中一次错误排查 Error:Error: Duplicate resources
		最近一直在使用react native中,遇到了很多的坑,同时也学习到了一些移动端的开发经验. 今天在做一个打包的测试时,遇到了一个问题,打包过程中报错“Error:Error: Duplicate ... 
随机推荐
- Android 导出db并查看内容
			1.导出sqlite的db文件: 使用工具DDMS,切换到DDMS,显示File Explorer窗口,找到/data/data/应用名/databases/数据库名,点击导出按钮,导出文件. 2.使 ... 
- 【HDOJ】3337 Guess the number
			神一样的题目.简言之,利用手段获得测试用例的第一行,输出结果.很显然利用wa, TLE, OLE等judge status可以获得测试用例.因此,果断Python写一个acm提交机器人.依赖lxml库 ... 
- Linq的延迟
			书名:LINQ: The Future of Data Access in C# 3.0 Learn LINQ and the C# 3.0 Features That Support It http ... 
- MyBatis学习总结2
			这一篇讲述MyBatis对数据库的CRUD操作,内容不做重复,只做添加:查看学习总结1 一.使用MyBatis对表执行CRUD操作——基于XML的实现 在SQL映射文件userMapper.xml中添 ... 
- 生成Excel錯誤 遠端程序呼叫失敗。 (發生例外狀況於 HRESULT: 0x800706BE)
			错误信息:详细信息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005.网上找到 ... 
- 《C#并行编程高级教程》第6章 PLINQ:声明式数据并行 笔记
			PLINQ这个话题好多书都写到过,这本也没有什么特别好的地方. 几个有用和有趣的点记录一下. 顺序的不确定性 用PLINQ就一定要记住并行后会导致顺序不确定的问题.解决方案就是AsOrdered或 ... 
- css滑动门制作圆角按钮
			之前做项目的时候,基本都是将圆角背景图切成三块,故而每次用的标签都超级多,a标签中总是包含三个span,然后里面还得放按钮,导航冗余标签极多. 事实上是之前理解的滑动门的精髓不够到位. 现在有两种方式 ... 
- [liu  yanling]软件测试分为哪几个计划过程阶段
			a) 计划阶段:编写测试计划,搭建测试环境,准备测试数据b) 设计阶段:编写测试用例(需求分析和测试用例文档)c) 执行阶段:执行测试用例,生成缺陷d) 报告阶段:测试报告,改进意见 
- linux下的rbenv和rails安裝
			今天是周一,我到新公司的第14天,今天继续linux下ruby和rails环境变量的配置. 首先碰到的问题是 主要看ubuntu下安装rbenv,ruby,rails开发环境, http://blog ... 
- mysql 资料总结 长期更新
			http://blog.csdn.net/ww1982_0_0_0/article/details/9169613 引入employess 
