[React Native] State and Touch Events

Source: Internet
Author: User

In React, components manage their own state. In this lesson, we'll walk through building a component which manages it ' s own state as well as using TextInput and Toucha Blehighlight to handle touch events.

Import React, {Component, proptypes} from ' React '; import {View, Text, StyleSheet, TextInput, Touchablehighlight, Activityindicatorios} from' React-native ';varstyle =stylesheet.create ({maincontainer: {flex:1, padding:30, MarginTop:65, Flexdirection:' Column ', Justifycontent:' Center ', BackgroundColor:' #48BBEC '}, Title: {marginbottom:20, FontSize:25, TextAlign:' Center ', Color:' #fff '}, Searchinput: {height:50, padding:4, MarginRight:5, FontSize:23, BorderWidth:1, BorderColor:' White ', Borderradius:8, Color:' White '}, ButtonText: {fontSize:18, Color:' #111 ', Alignself:' Center '}, Button: {height:45, Flexdirection:' Row ', BackgroundColor:' White ', BorderColor:' White ', BorderWidth:1, Borderradius:8, MarginBottom:10, Alignself:' Stretch ', Justifycontent:' Center '}}); Exportdefaultclass 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        }); Console.log (' Submit ', This. State.username); //fetch data from GitHub        //reroute to the next passing that GitHub informaiton} render () {return (           <view style={style.maincontainer}> <text Style={style.title}>search forA 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> </View>       )    }}

< TextInput                  style ={style.searchinput}                  value ={this.state.username}                  OnChange ={this.handlechange.bind (This)}                />

Search box, once value changed, set current username state.

< touchablehighlight       style ={style.button}       onpress ={this.handlesubmit.bind (This)}       Underlaycolor = "White" >

Search button, a touch button, so is not the OnClick event but Onpress event.

Underlaycolor:when Touch, change background color to white color.

[React Native] State and Touch Events

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.