React Native ——實現一個簡單的抓取github上的項目資料列表

來源:互聯網
上載者:User

標籤:

/** * Sample React Native App * https://github.com/facebook/react-native */‘use strict‘;var React = require(‘react-native‘);var {  AppRegistry,  StyleSheet,  Text,  Image,  View,  TextInput,  ListView,} = React;var GIT_URL = ‘https://api.github.com/search/repositories?q=‘;var AwesonProject = React.createClass({  /*--  lifecycle --*/  getInitialState: function() {    return {      // (row1, row2) => row1 !== row2:如果兩次的資料不同的話,告訴資料來源該資料發生了改變      dataSource: new ListView.DataSource({        rowHasChanged: (row1, row2) => row1 !== row2,      }),    };  },  render: function() {    var listViewContent;    if (this.state.dataSource.getRowCount() === 0) {      listViewContent =                        <Text>                          there‘s nothing to search , please have another key to tap.                        </Text>;    } else {      listViewContent =                        <ListView                          ref=‘listview‘                          dataSource={this.state.dataSource}                          renderRow={this.renderRow}                          automaticallyAdjustConntentInsets={false}                          keyboardShouldPersistTaps={true}                          showsVerticalScrollIndicator={true} />    }    return (      <View style={styles.container}>        <TextInput            autoCapitalize=‘none‘            autoCorrect={false}            placeholder=‘search forr git project...‘            onEndEditing={this.onSearchChange}            style={styles.searchView}>        </TextInput>        {listViewContent}      </View>    );  },  /*-- private method --*/  //點擊搜尋響應資料請求  onSearchChange: function(event) {    var serarchText = event.nativeEvent.text.toLowerCase();    var queryURL = GIT_URL + encodeURIComponent(serarchText);    fetch(queryURL)      .then((response) => response.json())      .then((responseData) => {        if (responseData.items) {          this.setState({            dataSource : this.state.dataSource.cloneWithRows(responseData.items)          });        }      })      .done();  },  //渲染列表中的每一行資料  renderRow: function(item) {    return (      <View>        <View  style={styles.row}>          <Image            source={{uri:item.owner.avatar_url}}            style={styles.Img}>          </Image>          <View>            <Text style={styles.name}>              {item.full_name}            </Text>            <Text style={styles.name}>              Star:{item.stargazers_count}            </Text>          </View>        </View>        <View style={styles.cellBorder}></View>      </View>    );  },});/*配置樣式*/var styles = StyleSheet.create({  container: {    flex: 1,    // justifyContent: ‘center‘,    // alignItems: ‘center‘,    backgroundColor: ‘#F5FCFF‘,  },  searchView:{    marginTop:30,    padding:5,    margin:5,    fontSize:15,    height:30,    backgroundColor:‘#EAEAEA‘  },  row:{    flexDirection:‘row‘,    padding:8,  },  name:{    marginBottom:8,    marginLeft:8,  },  Img:{    width:50,    height:50,  },  cellBorder:{    height:1,    marginLeft:2,    backgroundColor:‘#EAEAEA‘,  }});AppRegistry.registerComponent(‘AwesonProject‘, () => AwesonProject);

 

React Native ——實現一個簡單的抓取github上的項目資料列表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.