The the-the-do HTTP requests in React Native are with the Fetch API. In the this video we'll talk about the Fetch and how to work with promises.
As we build application components, we'll need to pass the data along as we change routes and bring them into view. With React Native We can does this easily and deliver the appropriate data to our Native Dashboard component.
Create Api.js:
Let API = { Getbio (username) { = username.tolowercase (). Trim (); = ' https://api.github.com/users/${username} '; return fetch (URL). Then (res=>Res.json ()); }, Getrepos (username) { = Username.tolowercase (). Trim (); = ' https://api.github.com/users/${username}/repos '; return fetch (URL). Then (res=>Res.json ()); = API;
In Main.js:
After button was clicked, call the API to fetch the data and pass the data as a props to the Dashboard component rendered T o The veiw by Navigatorios:
Handlesubmit (event) {//update our Indicatorios spinner This. SetState ({isloading:true }); //fetch data from GitHubApi.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