react-7 组件库 Ant Design Mobile(移动端)

 1.安装组件库

npm install --save antd-mobile

常用组件
tabbar 底部导航

Swiper 轮播图(走马灯)

NavBar(顶部返回累) 配合 Dialog,Toast

InfiniteScroll 无限滚动(实现下拉刷新)

Skeleton 骨架屏:用图形表示内容占位

SideBar 侧边导航

2. 按需导入
我们可以根据项目需要导入需要用到的组件, 该组件的样式会自动导入.

eg:

import { Button } from 'antd-mobile';

下载组件库,icon:特殊一点,导入使用

//下载
npm install --save antd-mobile-icons

//导入使用
import { AntOutline } from 'antd-mobile-icons'

常用组件:

封装: tabbar

import React, { Component } from 'react';
import './MyTabbar.scss'
import { Badge, TabBar } from 'antd-mobile'
import { AppOutline, AppstoreOutline, MessageFill, MessageOutline, UserOutline } from 'antd-mobile-icons'
import { withRouter } from 'react-router-dom';
interface Props {
    history:any,
    match:any,
    location:any,
}
interface State {
    tabs: Array<any>
}

class MyTabbar extends Component<Props, State> {
    constructor(props: Props) {
        super(props)
        this.state = {
            tabs: [
                {
                    key: '/index/home',
                    title: '首页',
                    icon: <AppOutline />,
                },
                {
                    key: '/index/mypage',
                    title: '分类',
                    icon: <AppstoreOutline />,
                },
                {
                    key: '/index/mycate',
                    title: '购物车',
                    icon: (active: boolean) =>
                        active ? <MessageFill /> : <MessageOutline />,
                },
                {
                    key: '/index/my',
                    title: '我的',
                    icon: <UserOutline />,
                },
            ]
        }
    }
    handleChange(key: string) {
        this.props.history.push(key)
    }
    render() {
        return (
            <TabBar className='tabbar' onChange={(key) => { this.handleChange(key) }}>
                {
                    this.state.tabs.map(item => (
                        <TabBar.Item key={item.key} icon={item.icon} title={item.title} />
                    ))
                }
            </TabBar>
        );
    }
}

export default withRouter(MyTabbar);
.tabbar{
    position: fixed;
    bottom: 0;
    .adm-tab-bar-wrap{
        width: 100%;
    }
}

 封装:swiper

拿到数据,通过组件传递给组件内数组。父向子

 接收使用

import React, { Component } from 'react';
import './index.scss'
import { Swiper } from 'antd-mobile'

interface Props {
    swiperlist: Array<any>
}
class MySwiper extends Component<Props> {
    render() {

        return (
            < Swiper className='myswiper' allowTouchMove={true} autoplay autoplayInterval={1000} loop >
                {
                    this.props.swiperlist.map((item, index) => {
                        return (
                            <Swiper.Item key={index}>
                                <img src={item.img} alt="" key={index} />
                            </Swiper.Item>
                        )
                    })
                }
            </Swiper>
        )
    }
}

export default MySwiper;
.myswiper{
    height: 200px;
    img{
        //height: 200px;
        width: 100%;
    }
}

navbar顶部返回:配合 Dialog,Toast

import React, { Component } from 'react';
import './index.scss'
import { NavBar,Dialog, Toast } from 'antd-mobile'
interface Props {
    match: any,
    location: any
}
class Detail extends Component<Props> {
    back = () =>{
        // Toast.clear();
        // Toast.show({
        //     content: '点击了返回',
        //     duration: 1000,
        // })
        Dialog.clear()    //关闭所有打开的对话框
        Dialog.confirm({
            content: '确定删除吗?',
            title:'警告',
        }).then((res)=>{
            if (res) {
                console.log('点击了确定,执行删除');
            } else {
                console.log('点击了取消');
            }
        })
    }
    render() {
        // 获取动态路由参数
        // console.log(this.props.match.params.id);
        // 获取固定路由参数,query,刷新页面参数会丢失
        // console.log(this.props.location.query.id);
        // 获取固定路由参数,state
        console.log(this.props.location.state.proid); //商品信息获取了
        return (
            <div className='detail'>
                <NavBar back='返回' onBack={() => { this.back()}}>
                    商品详情
                </NavBar>
            </div>
        );
    }
}
export default Detail;

下拉加载:无限滚动:需要放在列表下边

//下拉加载
import { InfiniteScroll } from 'antd-mobile'
//骨架屏
import { Skeleton } from 'antd-mobile'



{   // 骨架屏
    this.state.recommendlist.length == 0 &&
        <>
            <Skeleton.Title animated />
            <Skeleton.Paragraph lineCount={5} animated />
        </>
}



{/* 下拉加载:hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}
<InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />

loadMore是个异步函数:::

 代码如下:::

import React, { Component } from 'react';
import './index.scss'
import { pro_recommendlist } from '@/api/index'
import { withRouter } from 'react-router-dom';

// 导入 无限加载 和 骨架屏组件
import { Skeleton } from 'antd-mobile'
import { InfiniteScroll } from 'antd-mobile'
interface Props {
    history: any //表示可选的值
    match: any,
    location: any
}
interface State {
    recommendlist: Array<any>
    hasMore: any,
    count: any,
}
class List extends Component<Props, State> {
    constructor(Props: Props) {
        super(Props)
        this.state = {
            // list: []
            recommendlist: [],
            count: 0,
            hasMore: true,
        }
    }
    // async componentDidMount() {
    //     var res = await pro_recommendlist({ count: 1, limitNum: 10 })
    //     // console.log(res.data.data);
    //     if (res.data.code == 200) {
    //         this.setState({ recommendlist: res.data.data })
    //     }
    // }
    handleClick(e: any, proid: any) {
        this.props.history.push({ pathname: '/detail', state: { proid } })
        // console.log(proid);
    }
    // 滑动加载
    loadMore = async () => {
        // 请求下一条数据
        var res = await pro_recommendlist({ count: this.state.count + 1 })

        if (res.data.data.length < 12) {
            // console.log(res);
            this.setState({
                hasMore: false,//数据加载完成,设置false})
                recommendlist: [...this.state.recommendlist, ...res.data.data],
                count: this.state.count + 1
            })
        } else {
            this.setState({
                recommendlist: [...this.state.recommendlist, ...res.data.data],
                count: this.state.count + 1
            })
        }
    }
    render() {
        return (
            <ul className="list_ul">
                {
                    this.state.recommendlist.map((item, index) => {
                        return (
                            <li key={index} onClick={(e) => { this.handleClick(e, item.proid) }}>
                                <img src={item.img1} alt="" />
                                <p className='clear_ellipsis'>{item.proname}</p>
                                <p className='money'>¥<span >{item.originprice}</span></p>
                            </li>
                        )
                    })
                }
                {   // 骨架屏
                    this.state.recommendlist.length == 0 &&
                        <>
                            <Skeleton.Title animated />
                            <Skeleton.Paragraph lineCount={5} animated />
                        </>
                }
                {/* hasMore 是否有更多内容,当hasMore为true时,列表滚动到底部会自动执行loadMore对应的回调函数 */}

                <InfiniteScroll loadMore={this.loadMore} hasMore={this.state.hasMore} />
            </ul>
        );
    }
}

export default withRouter(List);

 骨架屏(两种写法)先导入,再使用

//骨架屏
import { Skeleton } from 'antd-mobile'


{
    this.state.recommendlist.length == 0 &&
    <>
        <Skeleton.Title animated />
        <Skeleton.Paragraph lineCount={5} animated />
    </>
}
                

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,在安装@ant-design/react-native时,不再需要手动链接(@ant-design/icons-react-native),而是可以使用自动链接功能。只需要使用命令`npm install @ant-design/icons-react-native --save-dev`来安装@ant-design/icons-react-native即可。此外,可以在`node_modules/@ant-design`文件夹下找到安装的文件。 根据引用,可以使用命令`react-native -v`来检查是否成功安装了react-native。 根据引用,示例代码展示了如何全局使用ant-design-mobile组件。官方文档中的示例是按需引入的,如果要全局使用,需要手动引入相应的组件文件。示例代码中引入了Button和InputItem组件,并在App组件中使用了这些组件。 所以,ant-design-mobile是一个基于React Native的UI组件,可以通过自动链接或手动引入组件文件的方式来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [react native 0.70版本使用ant-design-mobile-rn及icons字体图标](https://blog.csdn.net/weixin_43233914/article/details/126849915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [react native 使用ant-design-mobile-rn的icon字体](https://blog.csdn.net/weixin_43233914/article/details/119450451)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值