[React Native] Passing data when changing routes
The way you make HTTP requests in React Native is with the Fetch API. In this video we'll talk about Fetch and how to work with promises.
As we build application components, we will need to pass data along as we change routes and bring them into view. With React Native we can do this easily and deliver the appropriate data to our native Dashboard component.
Create api.js:
let api = {
getBio(username){
username = username.toLowerCase().trim();
let url = `https://api.github.com/users/${username}`;
return fetch(url).then(res=>res.json());
},
getRepos(username){
username = username.toLowerCase().trim();
let url = `https://api.github.com/users/${username}/repos`;
return fetch(url).then(res=>res.json());
}
};
module.exports = api;
In Main.js:
After button is clicked, call the api to fetch the data and pass the data as a props to the Dashboard component rendered to the veiw by NavigatorIOS:
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: ''
});
}
})
}
Dashboard.js:
import React, { Component } from 'react';
import {Text, View, StyleSheet} from 'react-native';
const styles = StyleSheet.create({
container: {
marginTop: 65,
flex: 1
},
image: {
height: 350
},
buttonText: {
fontSize: 24,
color: 'white',
alignSelf: 'center'
}
});
class Dashboard extends Component{
render(){
return (
<View style={styles.container}>
<Text>This is the dashboard</Text>
<Text>{JSON.stringify(this.props.userInfo)}</Text>
</View>
);
}
}
module.exports = Dashboard;

[React Native] Passing data when changing routes的更多相关文章
- [转] React Native Navigator — Navigating Like A Pro in React Native
There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...
- react native 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...
- React Native (一) 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.笔者 ...
- react native 学习之 native modules
翻译自https://facebook.github.io/react-native/docs/native-modules-ios.html Native Modules 很多情况下,app需要使用 ...
- Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native
Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- React Native:使用 JavaScript 构建原生应用
[转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...
- React Native之 Navigator与NavigatorIOS使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native之ListView使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
随机推荐
- 项目管理系统 SQL2005数据库查询CPU100%问题研究
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4595084.html] 在项目管理系统中出现查询工程明细出现CPU100%卡死症状: 1.打 ...
- 还是编码 汉字(GB2312和GBK)的ASCII码对照表
GB2312和GBK每一个汉字由2个字节组成,这2个字节的ASCII码大小分别是:gb2312: high8 = 0xa1-->0xfe (161 - 254)low8 = 0xa1--> ...
- solr的原子更新/局部更新
solr支持三种类型的原子更新: set - to set a field. add - to add to a multi-valued field. inc - to increment a fi ...
- Android USB Host 通信程序
换到了一家新公司,于是就有了新的项目.这次的项目 要用Android SDK与USB HID设备进行通信.第一次接触Android SDK,以及USB,记录下源程序.开发过程以及一些心得. 首先,要感 ...
- VCL+FMX 双剑合壁编程
VCL 是经典,FMX 是新生,新生事物总会带来一些好玩新奇的东西.舍弃经典是浪费,不了解新生事物是等死,那么我们来一个二合一双剑合壁又如何呢? 要双剑合壁,就得投些机,取些巧.由于 Delphi / ...
- 《ArcGIS Engine+C#实例开发教程》
原文:<ArcGIS Engine+C#实例开发教程> 摘要:<ArcGIS Engine+C#实例开发教程>,面向 ArcGIS Engine(以下简称AE)开发初学者,本教 ...
- TortoiseSVN设置比较工具为BeyondCompare
1. "C:\Beyond Compare 4\BCompare.exe" %base %mine /title1=%bname /title2=%yname /leftread ...
- BZOJ3530: [Sdoi2014]数数
3530: [Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 322 Solved: 188[Submit][Status] ...
- [HDU POJ] 逆序数
HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- POJ 3013
思路:ans = 每条边(u,v)*v的子树节点的w = 所有的dist[v]*w[v]之和; #include<iostream> #include<queue> #incl ...