开发一个基于React Native的简易demo--读取网络数据并展示

本文介绍了如何使用React Native进行网络请求,通过fetch获取数据并利用state保存。接着,详细讲解了如何将数据展示在ListView中,同时解决ListView与Swiper滑动冲突的问题。在组件挂载后加载ListView数据,确保Swiper正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

react-native的网络请求用fetch,及其简单,请求到的数据保存起来,react-native用state来保存数据,类似于Java的request,还可以传递给另一个类,所以就是:请求数据,赋值。展示数据这里用ListView,有点类似于Java的HashMap,要求唯一的key,一个key代表一条数据。

定义一个方法,接收fetch的数据,并赋值给state中的dataSource:

buttonTap=()=>{ 

    fetch( 'http://bbs.reactnative.cn/api/category/3'
    ).then((response)=>response.json())
    .then((jsondata) =>{
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

        this.setState({dataSource: ds.cloneWithRows(jsondata.topics)});
        this.setState({title:jsondata.description});
         //alert(jsondata);
    })
    .catch((error)=>{
      alert(error);
      console.warning(error);
    });

  };

由于ListView会跟Swiper有滑动冲突,所以,在用ListView渲染数据时,要做个判断componentDidMount(),等ListView数据加载完,再启用Swiper:

class RecentChatsScreen extends React.Component {

    // render() {
    //  return (
    //      <View style={[styles.containerSwiper]}>
  //         <View style={{flexDirection:'column',justifyContent:'center',flex:1,alignItems:'center'}}>
  //             <Text style={{color:'#fff',fontSize: 14,textAlign:'center'}}>当前尚未登录</Text>
  //             <Text style={{color:'#fff',fontSize: 14,textAlign:'center'}}>请点击界面登录</Text>
  //         </View>
  //     </View>
    //  );
  // }

  // 初始化模拟数据
  constructor(props) {
    super(props);
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      swiperShow:false,
      dataSource: ds.cloneWithRows([
        // 'John', 'Joel', 'James', 'Jimmy', 'Jackson', 'Jillian', 'Julie', 'Devin'
      ])
    };
    this.buttonTap();//初始化
  }

  componentDidMount(){
    setTimeout(()=>{
        this.setState({
            swiperShow:true
        });
    },0)
  }


  buttonTap=()=>{ 

    fetch( 'http://bbs.reactnative.cn/api/category/3'
    ).then((response)=>response.json())
    .then((jsondata) =>{
        console.log(jsondata); 
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

        this.setState({dataSource: ds.cloneWithRows(jsondata.topics)});
        this.setState({title:jsondata.description});
         //alert(jsondata);
    })
    .catch((error)=>{
      alert(error);
      console.warning(error);
    });

  };
  render() {
    if(this.state.swiperShow){ 
        return(
          <View style={[styles.containerSwiper]}>
        <ListView style={{flex:8} }
          removeClippedSubviews={false}
          dataSource={this.state.dataSource}
          renderRow={(rowData) => <CELL title={rowData.title} detailTitle={rowData.timestampISO}></CELL>}
          enableEmptySections={true} 
        />
      </View>
     )
    }else {
        return (
            <View style={{height:100}}>

            </View>
        );
    }
  }

}

这里写图片描述


下一篇:开发一个基于React Native的简易demo–源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值