1. 去掉customedList组件,避免增加代码复杂度

This commit is contained in:
xuwenyang 2019-05-18 15:06:41 +08:00
parent 762c46eead
commit beb5c0ec21
2 changed files with 90 additions and 119 deletions

View file

@ -1,97 +0,0 @@
import React, {Component} from 'react';
import {StyleSheet, Text, View, InteractionManager, FlatList} from 'react-native';
export default class CustomedList extends Component {
constructor(props) {
super(props);
this.listType = props.listType || 'undefined';
this.dataSource = props.dataSource;
this.state = {
listData: [],
hasMore: false,
refreshing: false,
refreshFailed: false
};
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
this.refresh();
});
}
refresh(loadMore = false) {
if (this.state.refreshing) {
return;
}
this.setState({hasMore: false, refreshing: true, refreshFailed: false});
this.dataSource.refresh(loadMore)
.then(result => {
if(!result) {
throw {
message: 'refresh ' + this.listType + ' no result'
}
} else {
console.log('refresh ' + this.listType + ' result:', result);
this.setState({
listData: result.list ? result.list : [],
hasMore: result.more,
refreshFailed: false
});
}
}).catch(e => {
this.setState({
listData: [],
hasMore: false,
refreshFailed: true
});
}).done(() => {
this.setState({
refreshing: false
});
});
}
loadMore() {
if (this.state.refreshing) {
return;
}
this.refresh(true);
}
render() {
return (
<FlatList
data={this.state.listData}
keyExtractor={(item, index) => {
return item.id ? item.id.toString() : index;
}}
renderItem={this.props.renderItem}
refreshing={this.state.refreshing}
onRefresh={this.refresh.bind(this)}
onEndReachedThreshold={5}
onEndReached={this.state.hasMore ? this.loadMore.bind(this) : null}
/>
);
}
}
const localStyle = StyleSheet.create({
});

View file

@ -6,7 +6,6 @@ import Touchable from '../touchable';
import Color from '../../style/color'; import Color from '../../style/color';
import UserIcon from '../userIcon'; import UserIcon from '../userIcon';
import CustomedList from '../customedList';
export default class FollowUserList extends Component { export default class FollowUserList extends Component {
@ -15,33 +14,102 @@ export default class FollowUserList extends Component {
super(props); super(props);
this.dataSource = props.dataSource; this.dataSource = props.dataSource;
this.listType = props.listType; this.listType = props.listType || 'undefined';
this.state = {
users: [],
hasMore: false,
refreshing: false,
refreshFailed: false
};
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
this.refresh();
});
}
refresh(loadMore = false) {
if (this.state.refreshing) {
return;
}
this.setState({hasMore: false, refreshing: true, refreshFailed: false});
this.dataSource.refresh(loadMore)
.then(result => {
if(!result) {
throw {
message: 'refresh ' + this.listType + ' no result'
}
} else {
console.log('refresh ' + this.listType + ' result:', result);
this.setState({
users: result.list ? result.list : [],
hasMore: result.more,
refreshFailed: false
});
}
}).catch(e => {
this.setState({
users: [],
hasMore: false,
refreshFailed: true
});
}).done(() => {
this.setState({
refreshing: false
});
});
}
loadMore() {
if (this.state.refreshing) {
return;
}
this.refresh(true);
} }
render() { render() {
return ( return (
<View style={localStyle.container}> <View style={localStyle.container}>
<CustomedList listType={this.props.listType} style={localStyle.list} <FlatList style={localStyle.list}
dataSource={this.props.dataSource} data={this.state.users}
renderItem={({item}) => { keyExtractor={(item, index) => {
return ( return item.id ? item.id.toString() : index;
<Touchable key={item.id} onPress={() => {}}> }}
<View style={localStyle.box}>
<UserIcon iconUrl={item.iconUrl}></UserIcon> renderItem={({item}) => {
<Text style={localStyle.userName}>{item.name}</Text> return (
<Touchable onPress={() => {}}> <Touchable key={item.id} onPress={() => {}}>
<Ionicons name="md-close" size={20} <View style={localStyle.box}>
style={localStyle.removeIcon} <UserIcon iconUrl={item.iconUrl}></UserIcon>
color={Color.inactiveText} /> <Text style={localStyle.userName}>{item.name}</Text>
</Touchable> <Touchable onPress={() => {}}>
</View> <Ionicons name="md-close" size={20}
</Touchable> style={localStyle.removeIcon}
); color={Color.inactiveText} />
}} </Touchable>
/> </View>
</View> </Touchable>
);
}}
refreshing={this.state.refreshing}
onRefresh={this.refresh.bind(this)}
onEndReachedThreshold={5}
onEndReached={this.state.hasMore ? this.loadMore.bind(this) : null}
/>
</View>
); );
} }
} }