diff --git a/List.js b/List.js index e32094d..433edcd 100644 --- a/List.js +++ b/List.js @@ -2,7 +2,6 @@ import React, { Component } from 'react'; import { FlatList, View, - Text, } from 'react-native'; import ListItem from './ListItem'; @@ -10,7 +9,6 @@ export default class List extends Component { constructor(props) { super(props); this.state = { - // 預設載入 isRefreshing: true, data: [], page: 0 @@ -22,11 +20,13 @@ export default class List extends Component { await this.getData(0) } - // 整理資料 + // 整理資料讓 ListItem 能使用 format = (array) => { return array.map((data) => { return { - // 整理資料格式符合 ListItem props + title: data.name, + desc: data.job_title, + image: data.avatar, } }) } @@ -34,18 +34,22 @@ export default class List extends Component { getData = async(page) => { try { // 這裡要記得改成自己電腦的 IP - const IP ='192.168.2.101'; + const IP ='192.168.57.1'; // 可以使用的 API // http://${IP}:1337/pokemons/1 // http://${IP}:1337/users/1 let response = await fetch(`http://${IP}:1337/users?_page=${page}&_limit=10`); let responseJson = await response.json(); - console.log('responseJson', responseJson); const data = this.format(responseJson); if (page === 0) { - // 第一筆資料,記得關掉 loading + this.setState({ + isRefreshing: false, + data, + }); } else { - // 滾動加載更新資料 + this.setState({ + data: [...this.state.data, ...data], + }); } return responseJson; } catch (e) { @@ -56,25 +60,25 @@ export default class List extends Component { render() { return ( { - // return 剛剛實作的 ListItem - return {item.title} - }} + data={this.state.data} + renderItem={({item}) => } onEndReached={() => { - // 滑到底部的時候加載新資料 + this.setState({ + page: this.state.page + 1 + }, () => { + this.getData(this.state.page) + }) }} refreshing={this.state.isRefreshing} onRefresh={() => { - // 下拉刷新 - }} - ItemSeparatorComponent={({highlighted}) => { - // return 簡單的分隔線 - return null; + this.setState({ + isRefreshing: true + }); + this.getData(0); }} + ItemSeparatorComponent={ + ({highlighted}) => + } /> ); } diff --git a/ListItem.js b/ListItem.js index 917e4c6..5e8b0b5 100644 --- a/ListItem.js +++ b/ListItem.js @@ -3,8 +3,7 @@ import { View, StyleSheet, Text, - Image, - TouchableOpacity, + Image } from 'react-native'; export default class ListItem extends Component { @@ -12,29 +11,39 @@ export default class ListItem extends Component { title: PropTypes.string, desc: PropTypes.string, image: PropTypes.string, - onPress: PropTypes.func, } static defaultProps = { title: '標題', desc: '內容', image: 'https://robohash.org/eaetin.png?size=150x150&set=set1', - onPress: () => {}, - } + } constructor(props) { super(props); } - + render() { return ( - + + - - {this.props.title} - - + + + + {this.props.title} + + + {this.props.desc} + + + ); } } @@ -42,9 +51,7 @@ export default class ListItem extends Component { const styles = StyleSheet.create({ container: { - flexDirection: 'row', - height: 100, - padding: 10, - backgroundColor: '#eee', + flex: 1, + padding: 30, }, }) \ No newline at end of file diff --git a/index.android.js b/index.android.js index e9dbcd6..6b72596 100644 --- a/index.android.js +++ b/index.android.js @@ -26,7 +26,7 @@ import List from './List'; export default class sample1 extends Component { render() { return ( - + ); } } diff --git a/sample/fetch/index.js b/sample/fetch/index.js index e383956..4ebad33 100644 --- a/sample/fetch/index.js +++ b/sample/fetch/index.js @@ -21,7 +21,7 @@ export default class FetchSample extends Component { getData = async(page) => { try { // 這裡要記得改成自己電腦的 IP - const IP ='192.168.2.101'; + const IP ='192.168.57.1'; let response = await fetch(`http://${IP}:1337/users/1`); let responseJson = await response.json(); console.log(responseJson);