taro 封装一个加载中组件

### 一.正常使用loading

1.组件中引入
import { AtSearchBar, AtActivityIndicator } from 'taro-ui';
  const loading = useSelector(store => store.loading);
return{listParams?.page === 1 && loading.effects['shipOrderList/fetchShipOrderList'] ? (
                <AtActivityIndicator size="38" mode="center" content="努力加载中..." />
            ) : (
                <View className="customer-wrapper">
                    {shipOrderList?.length ? (
                        <>
                            {shipOrderList?.map(item => {
                                return <OrderInfo key={item.id} item={item} />;
                            })}
                        </>
                    ) : (
                        <Empty />
                    )}
                    <ActivityIndicator
                        loading={loading.effects['shipOrderList/fetchShipOrderList']}
                    />
                </View>
            )}
            }

二.公共组件

components/ActivityIndicator/

import React from 'react';
import { AtActivityIndicator } from 'taro-ui';
import { View } from '@tarojs/components';

export default function ActivityIndicator(props) {
    return (
        <>
            {props.loading ? (
                <View
                    style={{
                        display: 'flex',
                        justifyContent: 'center',
                        paddingTop: '10px',
                        paddingBottom: '10px',
                    }}
                >
                    <AtActivityIndicator content="加载中..." />
                </View>
            ) : null}
        </>
    );
}

三.hooks

src/utils/hooks/useLoading.js

import { useSelector } from 'react-redux';

export const LoadingType = {
    EFFECT: 'effects',
    MODEL: 'models',
    GLOBAL: 'global',
};

const useLoading = (targets = [], type = LoadingType.EFFECT) => {
    const loadingState = useSelector(state => state.loading) || {};
    let isLoading = false;
    if (type in loadingState) {
        isLoading =
            type === LoadingType.GLOBAL
                ? !!loadingState[type]
                : targets.some(target => !!loadingState[type][target]);
    }
    return isLoading;
};

export default useLoading;

四.详情页等使用组件

src/components/PageWrapper/index.js

import React from 'react';
import { View } from '@tarojs/components';
import { AtActivityIndicator } from 'taro-ui';
import useLoading from '@/utils/hooks/useLoading';

const PageWrapper = ({ effects, children }) => {
    const loading = useLoading(effects);
    return (
        <View>
            {loading ? (
                <AtActivityIndicator
                    className="loading"
                    size="38"
                    mode="center"
                    content="努力加载中..."
                />
            ) : (
                children
            )}
        </View>
    );
};

export default PageWrapper;

使用

import PageWrapper from '@/components/PageWrapper';
const DEFAULT_TEXT = '-';
export default () => {
return (
    <PageWrapper effects={['shipOrderDetail/fetchShipOrderDetail']}>
            <View className="container">
                <View className="section-title">基本信息</View>
           	}
           	</PageWrapper>
           	)

或者直接引入

import { AtButton, AtActivityIndicator } from 'taro-ui';
import Empty from '@/components/Empty';
import useLoading from '@/utils/hooks/useLoading';
const Product = () => {
    const dispatch = useDispatch();
    const productSelect = useSelector(state => state.shipOrderProductSelect);
    const loading = useLoading(['shipOrderProductSelect/fetchProductInfo']);
    useDidShow(() => {
        dispatch({ type: 'shipOrderProductSelect/fetchProductInfo' });
    });
    }
      return !loading ? (
        <View className="product-select__wrap">
        内容
        </View>
         ) : (
        <AtActivityIndicator className="loading" size="38" mode="center" content="努力加载中..." />
    );

缺省组件

src/components/Empty/index.jsx

import React from 'react';
import { View, Image } from '@tarojs/components';

import empty from '@/assets/empty.png';

import './index.scss';

export default function Empty(props) {
    const { content } = props;
    return (
        <View className='empty-wrapper'>
            <Image src={empty} />
            <View>{content || '暂无结果'}</View>
        </View>
    )
}

//index.scss

.empty-wrapper {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    color: #b8bec8;
    font-size: 28px;
    text-align: center;
    transform: translate(-50%, -50%);
    image {
        width: 190px;
        height: 188px;
        margin-bottom: 30px;
        margin-left: 30px;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值