Advertisement
Guest User

React

a guest
Jan 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { View, TextInput, ListView, Button } from 'react-native';
  3.  
  4. export default class Github extends Component {
  5. constructor(props) {
  6. super(props);
  7. const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
  8. this.state = {
  9. text: '',
  10. error: false,
  11. dataSource: ds.cloneWithRows()
  12. }
  13. this.handleChange = this.handleChange.bind(this);
  14. }
  15. fetchRepos(e) {
  16. this.state.dataSource = getUserRepos(this.state.text).then();
  17. }
  18.  
  19. render() {
  20. return() {
  21. <View>
  22. <TextInput
  23. onChangeText={(text) => this.setState({text})}
  24. placeholder={'Username'}
  25. value={this.state.text}
  26. />
  27. <Button
  28. onPress={fetchRepos}
  29. >
  30. Fetch
  31. </Button>
  32. <ListView
  33. dataSource={this.state.dataSource}
  34. renderRow={(rowData) => <Text>{rowData}</Text>}
  35. />
  36. </View>
  37. }
  38. }
  39. }
  40.  
  41. export const getUserRepos = (name) => {
  42. let username = name.toLowerCase().trim();
  43. const URL = `https://api.github.com/users/${username}/repos?sort=updated`;
  44. return fetch(URL).then((res) => res.json());
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement