1 回答
TA贡献2016条经验 获得超9个赞
当您滚动到底部时,您正在调用一个超时的异步函数。该超时将覆盖您的以下代码并将 loading 再次设置为 true。所以在这种情况下加载永远不会是错误的。
} else if (this.state.page === this.state.last_page) {
this.setState({loading: false}, () =>
console.log('if--loading', this.state.loading), // log false!! that's mean a spinner should disapeare
);
}
你在这里需要两件事。
1) 尝试在你的 catch 块中将 loading 设置为 false。
} catch (err) {
console.log('err', err);
this.setState({loading: false});
}
2) 在您的状态中添加另一个isAllDataFetched初始值为 false 的值。当您从 API 接收到空数据时,将 loading 设置为 false。不确定您的数据如何,但可以执行以下操作;
getData = async () => {
try {
this.setState({loading: true});
let response = await API.get(`/tracks?page=${this.state.page}`);
let lastPage = response.data.data.items.last_page;
let {
data: {
data: {
items: {data},
},
},
} = response;
// if it's an array
if(data.length === 0) {
this.setState({loading: false, isAllDataFetched: true});
}
//...
} catch (err) {
console.log('err', err);
}
};
最后,在您的 handleLoadMore 方法中添加以下行。
handleLoadMore = () => {
if(this.state.isAllDataFetched) return;
我为你创建了一个演示。您可以按照此逻辑使其工作。它与你所拥有的有点不同,但我认为它会有所帮助。
这是代码。
import React from 'react';
import {
View, Text, FlatList, ActivityIndicator, SafeAreaView
} from 'react-native';
class App extends React.PureComponent {
state = {
songs: [
{
userId: 1,
id: 1,
title: 'delectus aut autem 1',
completed: false,
},
{
userId: 1,
id: 2,
title: 'delectus aut autem 2',
completed: false,
},
],
loading: false,
page: 3,
totalPage: 10,
};
componentDidMount() {
this.getData();
}
getData = async () => {
const { songs } = this.state;
try {
this.setState({ loading: true });
const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${this.state.page}`, {
headers: {
'Content-Type': 'application/json',
},
});
const json = await response.json();
this.setState({
songs: [...songs, json],
});
this.setState({ loading: false });
} catch (err) {
console.log('err', err);
this.setState({ loading: false });
}
};
renderItems = ({ item }) => (
<Text style={{ backgroundColor: 'blue', height: 200, marginBottom: 5 }}>{`${item.title}-${item.id}`}</Text>
);
onEndReached = () => {
const { page, loading, totalPage } = this.state;
if (loading) return;
if (page <= totalPage) {
this.setState({ loading: true, page: page + 1 }, () =>
setTimeout(() => {
this.getData();
}, 2000));
} else {
this.setState({ loading: false });
}
}
renderFooter = () => {
const { loading } = this.state;
if (loading) {
return (
<View>
<ActivityIndicator color="#000" size="large" />
</View>
);
}
return null;
}
renderListEmptyComponent = () => <View />;
render() {
const { songs } = this.state;
return (
<SafeAreaView style={{ flex: 1, backgroundColor: 'red' }}>
<FlatList
data={songs}
keyExtractor={song => song.id}
initialNumToRender={10}
contentContainerStyle={{ flexFrow: 1, backgroundColor: 'white' }}
style={{ flex: 1 }}
ListEmptyComponent={this.renderListEmptyComponent}
renderItem={this.renderItems}
onEndReached={this.onEndReached}
onEndReachedThreshold={0.7}
ListFooterComponent={this.renderFooter}
/>
</SafeAreaView>
);
}
}
export default App;
这里有一个工作演示(使用 iOS 设备)
添加回答
举报