Javascript 如何从数据库中fech数据apihttps://covid19.mathdro.id/api?? 我正在尝试在我的app.js中删除这些数据 从“React”导入React; 从“react native”导入{View,Text,FlatList}; 类全局扩展React.Component{ 构造函数(){ 超级(); 此.state={ 全球:[], 刷新:错误 } } renderItem=({item})=>( Positif:{item.confirmed.value} Sembuh:{item.recovered} Meninggal:{项目.死亡} ) onRefresh=()=>{ 这是getDataApi(); } componentDidMount=()=>{ 这是getDataApi(); } getDataApi=async()=>{ this.setState({刷新:true}) 取('https://covid19.mathdro.id/api') .then(response=>response.json()) .then(json=>this.setState({global:json.confirm.value})) } render(){ console.log(this.state.global); 返回( item.toString()} renderItem={this.renderItem} 刷新={this.state.refreshing} onRefresh={this.onRefresh} /> ) } } 导出默认全局;
这是我的代码您从API得到的响应是一个对象,需要使用flatlist,因为您在此处仅显示三个文本,您可以执行以下操作:Javascript 如何从数据库中fech数据apihttps://covid19.mathdro.id/api?? 我正在尝试在我的app.js中删除这些数据 从“React”导入React; 从“react native”导入{View,Text,FlatList}; 类全局扩展React.Component{ 构造函数(){ 超级(); 此.state={ 全球:[], 刷新:错误 } } renderItem=({item})=>( Positif:{item.confirmed.value} Sembuh:{item.recovered} Meninggal:{项目.死亡} ) onRefresh=()=>{ 这是getDataApi(); } componentDidMount=()=>{ 这是getDataApi(); } getDataApi=async()=>{ this.setState({刷新:true}) 取('https://covid19.mathdro.id/api') .then(response=>response.json()) .then(json=>this.setState({global:json.confirm.value})) } render(){ console.log(this.state.global); 返回( item.toString()} renderItem={this.renderItem} 刷新={this.state.refreshing} onRefresh={this.onRefresh} /> ) } } 导出默认全局;,javascript,expo,Javascript,Expo,这是我的代码您从API得到的响应是一个对象,需要使用flatlist,因为您在此处仅显示三个文本,您可以执行以下操作: import React from 'react'; import {View , Text, FlatList } from 'react-native'; class Global extends React.Component{ constructor(){ super(); this.state = {
import React from 'react';
import {View , Text, FlatList } from 'react-native';
class Global extends React.Component{
constructor(){
super();
this.state = {
global: [],
refreshing: false
}
}
renderItem = ({item}) => (
<View style={{ padding: 20, borderBottomWidth: 1, borderBottomColor: '#000'}}>
<Text> Positif: {item.confirmed.value} </Text>
<Text> Sembuh: {item.recovered} </Text>
<Text> Meninggal: {item.deaths} </Text>
</View>
)
onRefresh = () => {
this.getDataApi();
}
componentDidMount = () => {
this.getDataApi();
}
getDataApi = async () => {
this.setState({ refreshing: true})
fetch('https://covid19.mathdro.id/api')
.then(response => response.json())
.then(json => this.setState({ global: json.confirmed.value }))
}
render(){
console.log(this.state.global);
return (
<View>
<FlatList
data={this.state.global}
keyExtractor= {item => item.toString()}
renderItem= {this.renderItem}
refreshing={this.state.refreshing}
onRefresh={this.onRefresh}
/>
</View>
)
}
}
export default Global;
render(){
返回(
{this.state.global.confirmed!=未定义?Positif:{this.state.global.confirmed.value}:null}
{this.state.global.confirm!=未定义?Sembuh:{this.state.global.recovered.value}:null}
{this.state.global.confirm!=未定义?Meninggal:{this.state.global.deathers.value}:null}
希望这有帮助!需要更多信息,您的代码当前发生了什么?您能发布您收到的错误吗?
render(){
return (
<View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
{this.state.global.confirmed != undefined ? <Text> Positif: {this.state.global.confirmed.value} </Text> : null}
{this.state.global.confirmed != undefined ? <Text> Sembuh: {this.state.global.recovered.value} </Text> : null}
{ this.state.global.confirmed != undefined ? <Text> Meninggal: {this.state.global.deaths.value} </Text> : null}
</View>