Listview 对API调用传递参数进行本机无限滚动反应
我有以下代码, 它可以很好地工作,但是它可以一次获取数据库中的所有线程(我可以有上千个线程)。我想每次滚动到屏幕底部时渲染25个线程。Listview 对API调用传递参数进行本机无限滚动反应,listview,reactjs,react-native,infinite-scroll,Listview,Reactjs,React Native,Infinite Scroll,我有以下代码, 它可以很好地工作,但是它可以一次获取数据库中的所有线程(我可以有上千个线程)。我想每次滚动到屏幕底部时渲染25个线程。 如何集成它并将参数传递给API调用 import React, { Component } from 'react'; import { AppRegistry, ListView, View, Text } from 'react-native'; import axios from 'axios'; export default class Compone
如何集成它并将参数传递给API调用
import React, { Component } from 'react';
import { AppRegistry, ListView, View, Text } from 'react-native';
import axios from 'axios';
export default class Component5 extends Component {
constructor() {
super();
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
userDataSource: ds,
};
}
componentDidMount() {
this.fetchUsers();
}
fetchUsers() {
axios.get('https://stageapi6.devmn.net/api/v1/forums/threads?topic_id=2418&per=25&page=2', {
headers: {
'client-id': '*****'
}
})
.then(response => this.setState({ userDataSource: this.state.userDataSource.cloneWithRows(response.data) }));
}
renderRow = (user) => {
console.log(user);
return (
<View>
{user.map(u =>
(<View key={u.thread.id} style={styles.containerStyle}>
<Text style={styles.whatchedStyle}>{u.thread.num_posts}</Text>
<View>
<Text style={[styles.textStyle, styles.topicStyle]}>{u.thread.topic_name}</Text>
<Text style={[styles.textStyle, styles.nameStyle]}>{u.thread.name}</Text>
</View>
</View>))
}
</View>
);
}
render() {
return (
<ListView
style={{ backgroundColor: '#8B008B' }}
dataSource={this.state.userDataSource}
renderRow={this.renderRow.bind(this)}
/>
);
}
}
const styles = {
containerStyle: {
backgroundColor: 'purple',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 2,
elevation: 1,
borderBottomWidth: 1,
borderBottomColor: '#8B008B',
padding: 10,
flexWrap: 'wrap',
alignItems: 'flex-start',
flexDirection: 'row',
},
textStyle: {
color: '#FFFFFF'
},
topicStyle: {
fontWeight: 'bold',
},
nameStyle: {
fontSize: 20
},
whatchedStyle: {
color: 'purple',
backgroundColor: '#FFFFFF',
padding: 10,
marginRight: 20
}
};
AppRegistry.registerComponent('Component5', () => Component5);
import React,{Component}来自'React';
从“react native”导入{AppRegistry,ListView,View,Text};
从“axios”导入axios;
导出默认类Component5扩展组件{
构造函数(){
超级();
const ds=new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
此.state={
userDataSource:ds,
};
}
componentDidMount(){
this.fetchUsers();
}
fetchUsers(){
axios.get()https://stageapi6.devmn.net/api/v1/forums/threads?topic_id=2418&per=25&page=2', {
标题:{
“客户端id”:“*****”
}
})
.then(response=>this.setState({userDataSource:this.state.userDataSource.cloneWithRows(response.data)});
}
renderRow=(用户)=>{
console.log(用户);
返回(
{user.map(u=>
(
{u.thread.num_posts}
{u.thread.topic_name}
{u.thread.name}
))
}
);
}
render(){
返回(
);
}
}
常量样式={
集装箱运输方式:{
背景颜色:“紫色”,
阴影颜色:“#000”,
阴影偏移:{宽度:0,高度:2},
阴影不透明度:0.1,
阴影半径:2,
立面图:1,
边界宽度:1,
borderBottomColor:“#8B008B”,
填充:10,
flexWrap:“wrap”,
alignItems:'flex start',
flexDirection:'行',
},
文本样式:{
颜色:“#FFFFFF”
},
主题风格:{
fontWeight:'粗体',
},
名称样式:{
尺寸:20
},
什么样的风格:{
颜色:'紫色',
背景颜色:“#FFFFFF”,
填充:10,
marginRight:20
}
};
AppRegistry.registerComponent('Component5',()=>Component5);
您可以使用ListView的onedReaded功能。当所有行都已呈现且列表已滚动到底部的onedreachedthreshold内时,将调用该函数。提供了本机滚动事件