react-native - 有没有办法限制在调用 URL 的响应中返回的 Web 数据量?
问题描述
有没有办法限制在调用 URL 的响应中返回的 Web 数据量?
或者有没有办法让它在滚动加载的组件时刷新以获取更多数据?
let { url, timeout } = urlConfig.getStuffc;
url = `${url}?${constructQueryFromArr(statusQueryString[status], 'status')}`;
httpRequest('get', url, { config: { timeout } }).then((response) => {
dispatch(showSpinner(false));
} //do something
解决方案
这是在单个请求中使用有限数据Flatlist
实现的基本示例API
export default class App extends Component {
constructor(props) {
super(props);
this.page = 1;
this.state = {
loading: false, // user list loading
isRefreshing: false, //for pull to refresh
data: [], //user list
error: ''
}
}
componentWillMount() {
this.fetchUser(this.page) //Method for API call
}
fetchUser(page) {
const url = `https://api.example.com?page=${page}`;
this.setState({ loading: true })
axios.get(url)
.then(res => {
let listData = this.state.data;
let data = listData.concat(res.data.items) . //concate list with response
this.setState({ loading: false, data: data })
})
.catch(error => {
this.setState({ loading: false, error: 'Something just went wrong' })
});
}
handleLoadMore = () => {
if (!this.state.loading) {
this.page = this.page + 1; // increase page by 1
this.fetchUser(this.page); // method for API call
}
};
render() {
if (this.state.loading && this.page === 1) {
return <View style={{
width: '100%',
height: '100%'
}}><ActivityIndicator style={{ color: '#000' }} /></View>;
}
return (
<View style={{ width: '100%', height: '100%' }}>
<FlatList
data={this.state.data}
extraData={this.state}
renderItem={({ item }) => (
<View>
....
</View>
)}
keyExtractor={(item, index) => index.toString()}
onEndReachedThreshold={0.4}
onEndReached={this.handleLoadMore.bind(this)}
/>
</View>
);
}
}
推荐阅读
- r - Ccraping with rvest - 按 div id 搜索
- r-markdown - bookdown 中围栏 div 块的自定义乳胶处理程序
- reactjs - TypeScript Styled-Components Theme 属性返回未定义
- javascript - 如何将本地图像文件上传到 Clarifai API
- c# - 如何获取自动完成功能不仅可以搜索开头,还可以使用包含,例如包含“门”
- c# - 如何测试对象是否为字符串?
- r - colMeans(x, na.rm = TRUE) 中的错误:“x”必须是数字
- pandas - 来自列表元素的熊猫新数据框
- reactjs - 为什么反应'stl-viewer'没有加载我的私人自定义python烧瓶api提供的stl文件
- python - Python - 替换字符串的一部分