[React Native] Passing data when changing routes

Source: Internet
Author: User

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&gt ; {Json.stringify ( This. Props.userinfo)}</text> </View>      ); }}module.exports= Dashboard;

[React Native] Passing data when changing routes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.