React native 反应本机实时问题
我正在从一个API获取数据到一个平面列表,它工作正常。问题是它不是一个实时获取,我必须刷新屏幕才能看到最近添加的数据。如何使抓取实时进行React native 反应本机实时问题,react-native,real-time,react-native-flatlist,React Native,Real Time,React Native Flatlist,我正在从一个API获取数据到一个平面列表,它工作正常。问题是它不是一个实时获取,我必须刷新屏幕才能看到最近添加的数据。如何使抓取实时进行 export default class CategoryScreen extends Component { constructor(props) { super(props); this.state = { categories: [] } } componentWillMount() { //Fetch recent products cons
export default class CategoryScreen extends Component {
constructor(props) {
super(props);
this.state = {
categories: []
}
}
componentWillMount() {
//Fetch recent products
const url = url
fetch(url)
.then((response) => response.json())
.then((responseJson) => {
this.setState({
categories: responseJson.categories
})
})
}
rederItem = ({ item }) => {
return (
<CategoryFlatList title={item.cat_name} image={item.img}/>
)
}
render() {
return (
<SafeAreaView style={{ flex: 1 }}>
<FlatList style={{}}
data={this.state.categories}
renderItem={this.rederItem}
numColumns={2}
keyExtractor={item => item.id}
/>
</SafeAreaView>
);
}
}
导出默认类类别屏幕扩展组件{
建造师(道具){
超级(道具);
此.state={
类别:[]
}
}
组件willmount(){
//获取最新产品
常量url=url
获取(url)
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
类别:responseJson.categories
})
})
}
重新编写=({item})=>{
返回(
)
}
render(){
返回(
项目id}
/>
);
}
}
如果我正确理解您的问题,您需要在服务器上更改数据时刷新数据!
如果是这种情况,它没有在列表上更新的原因是您没有调用fetch,当您刷新屏幕时,fetch被调用,数据被更新
有多种解决方案可解决此问题:
以下是您可以做的事情: 1) 制作一个子机制 2) 使用pull刷新平面列表 3) 使用轮询,您可以使用Redux Sagas或Redux Observable(这是首选方法)进行轮询,也可以使用如下计时器进行轮询 这就是你开始间歇的方式
this.intervalID = setInterval(() => {
// do something
}, 5000) //time for polling in millies
这就是你停止间歇的方法
if (this.intervalID != null) {
clearInterval(this.intervalID)
this.intervalID = null
}
我不确定我是否理解你的意思。是否要多次调用fetch?我希望同步获取数据,每当我向Json文件中添加数据时,我希望在不刷新页面的情况下显示已添加的新数据。1。如何向json文件添加数据?2.每次向json文件添加数据时,是否调用fetch?我是从仪表板而不是从appOk添加数据。我看到其他人已经回复了。。。祝你好运