最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React?Native實現(xiàn)Toast輕提示和loading效果

 更新時間:2023年09月02日 09:02:01   作者:Lanny-Chung  
這篇文章主要介紹了React Native實現(xiàn)Toast輕提示和loading效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

使用react native的小伙伴都知道,官方并未提供輕提示組件,只提供了ToastAndroid API,顧名思義,只能再安卓環(huán)境下使用,對于ios就愛莫能助,故此,只能通過官方的核心組件,自行封裝,實現(xiàn)Toast功能。

實現(xiàn)

創(chuàng)建文件

首先我們需要創(chuàng)建一個Toast組件,引入對應(yīng)需要的依賴,icon等等
聲明數(shù)據(jù)類型,通用方法

import React, {Component} from 'react';
import {View, Text, StyleSheet, Animated, Easing} from 'react-native';
import icon_success from '../assets/images/icon-success.png';
import icon_error from '../assets/images/icon-error.png';
import icon_loading from '../assets/images/icon-loading.png';
import icon_warning from '../assets/images/icon-warning.png';
type StateType = {
  isVisible: boolean;
  icon: any;
  message: string;
};
type ParamsType = string | {message: string; duration?: number};
function getParams(data: ParamsType): {message: string; duration: number} {
  let msg!: string;
  let dur!: number;
  if (typeof data === 'string') {
    msg = data;
    dur = 2000;
  } else {
    msg = data.message;
    dur = data.duration != null ? data.duration : 2000;
  }
  return {
    message: msg,
    duration: dur,
  };
}

實現(xiàn)樣式和UI層次渲染

我們需要創(chuàng)建一個class,接收參數(shù),并根據(jù)不同的條件渲染:success、error、warning、show、loading等
并拋出自己的實例

class ToastComponent extends Component<{} | Readonly<{}>, StateType> {
  timeout!: NodeJS.Timeout;
  rotate: Animated.Value = new Animated.Value(0);
  constructor(props: {} | Readonly<{}>) {
    super(props);
    this.state = {
      isVisible: false,
      icon: null,
      message: '',
    };
    Toast.setToastInstance(this);
  }
  showToast(icon: any, message: string, duration: number) {
    this.setState({
      isVisible: true,
      icon,
      message,
    });
    if (duration !== 0) {
      const timeout = setTimeout(() => {
        this.closeToast();
      }, duration);
      this.timeout = timeout;
    }
  }
  showRotate() {
    Animated.loop(
      Animated.timing(this.rotate, {
        toValue: 360,
        duration: 1000,
        easing: Easing.linear,
        useNativeDriver: true,
      }),
    ).start();
  }
  closeToast() {
    this.setState({
      isVisible: false,
      icon: null,
      message: '',
    });
    if (this.timeout) {
      clearTimeout(this.timeout);
    }
  }
  render() {
    const {isVisible, icon, message} = this.state;
    return isVisible ? (
      <View style={style.root}>
        <View style={[style.main, icon === null ? null : style.mainShowStyle]}>
          {icon && (
            <Animated.Image
              style={[
                style.icon,
                {
                  transform: [
                    {
                      rotate: this.rotate.interpolate({
                        inputRange: [0, 360],
                        outputRange: ['0deg', '360deg'],
                      }),
                    },
                  ],
                },
              ]}
              source={icon}
            />
          )}
          <Text style={style.tip}>{message}</Text>
        </View>
      </View>
    ) : null;
  }
}
const style = StyleSheet.create({
  root: {
    height: '100%',
    backgroundColor: 'transparent',
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    zIndex: 99999,
    alignItems: 'center',
    justifyContent: 'center',
  },
  main: {
    maxWidth: 200,
    maxHeight: 200,
    backgroundColor: '#00000099',
    borderRadius: 8,
    alignItems: 'center',
    justifyContent: 'center',
    padding: 20,
  },
  mainShowStyle: {
    minWidth: 140,
    minHeight: 140,
  },
  icon: {
    width: 36,
    height: 36,
    resizeMode: 'cover',
    marginBottom: 20,
  },
  tip: {
    fontSize: 14,
    color: '#fff',
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

拋出對外調(diào)用的方法

此時我們需要再聲明一個class,對外拋出方法以供調(diào)用
最后導(dǎo)出即可

class Toast extends Component<{} | Readonly<{}>, {} | Readonly<{}>> {
  static toastInstance: ToastComponent;
  static show(data: ParamsType) {
    const {message, duration} = getParams(data);
    this.toastInstance.showToast(null, message, duration);
  }
  static loading(data: ParamsType) {
    const {message, duration} = getParams(data);
    this.toastInstance.showToast(icon_loading, message, duration);
    this.toastInstance.showRotate();
  }
  static success(data: ParamsType) {
    const {message, duration} = getParams(data);
    this.toastInstance.showToast(icon_success, message, duration);
  }
  static error(data: ParamsType) {
    const {message, duration} = getParams(data);
    this.toastInstance.showToast(icon_error, message, duration);
  }
  static warning(data: ParamsType) {
    const {message, duration} = getParams(data);
    this.toastInstance.showToast(icon_warning, message, duration);
  }
  static clear() {
    if (this.toastInstance) {
      this.toastInstance.closeToast();
    }
  }
  static setToastInstance(toastInstance: ToastComponent) {
    this.toastInstance = toastInstance;
  }
  render() {
    return null;
  }
};
export {Toast, ToastComponent};

組件掛載

我們需要將UI層組件在入口TSX文件進(jìn)行掛載,不然Toast無法渲染

/* APP.tsx */
import React from 'react';
import {StatusBar} from 'react-native';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {ToastComponent} from './src/components/Toast';
const Stack = createStackNavigator();
function App(): JSX.Element {
  return (
    <SafeAreaProvider>
      <StatusBar barStyle="dark-content" backgroundColor="#EAF7FF" />
      <ToastComponent />
    </SafeAreaProvider>
  );
}
export default App;

API調(diào)用

掛載完成,接下來,在我們需要用到的地方,調(diào)用即可

import {Toast} from '../../components/Toast';
// 
Toast.success('登錄成功');
Toast.error('密碼錯誤');
Toast.warning('我是警告');
Toast.loading('加載中,請稍后');
Toast.loading({message: "我是不關(guān)閉的Toast", duration: 0})
Toast.success({message: "我是2秒后關(guān)閉的Toast", duration: 2000});
Toast.clear(); // 手動關(guān)閉

到此這篇關(guān)于React Native實現(xiàn)Toast輕提示和loading的文章就介紹到這了,更多相關(guān)React Native實現(xiàn)Toast輕提示和loading內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React語法中設(shè)置TS校驗規(guī)則的步驟詳解

    React語法中設(shè)置TS校驗規(guī)則的步驟詳解

    這篇文章主要給大家介紹了React語法中如何設(shè)置TS校驗規(guī)則,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-10-10
  • react 權(quán)限樹形結(jié)構(gòu)實現(xiàn)代碼

    react 權(quán)限樹形結(jié)構(gòu)實現(xiàn)代碼

    這篇文章主要介紹了react 權(quán)限樹形結(jié)構(gòu)實現(xiàn)代碼,項目背景react + ant design,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-05-05
  • 關(guān)于React中setState同步或異步問題的理解

    關(guān)于React中setState同步或異步問題的理解

    相信很多小伙伴們都一直在疑惑,setState 到底是同步還是異步。本文就詳細(xì)的介紹一下React中setState同步或異步問題,感興趣的可以了解一下
    2021-11-11
  • React中實現(xiàn)組件通信的幾種方式小結(jié)

    React中實現(xiàn)組件通信的幾種方式小結(jié)

    在構(gòu)建復(fù)雜的React應(yīng)用時,組件之間的通信是至關(guān)重要的,從簡單的父子組件通信到跨組件狀態(tài)同步,不同組件之間的通信方式多種多樣,下面我們認(rèn)識react組件通信的幾種方式,需要的朋友可以參考下
    2024-04-04
  • React+redux項目搭建流程步驟分析

    React+redux項目搭建流程步驟分析

    本文介紹了如何搭建一個React項目,包括創(chuàng)建項目、去除無用文件夾、配置項目、安裝craco擴(kuò)展webpack配置、配置代碼格式化、創(chuàng)建路由、集成Redux等步驟,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • React中關(guān)于render()的用法及說明

    React中關(guān)于render()的用法及說明

    這篇文章主要介紹了React中關(guān)于render()的用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • React styled-components設(shè)置組件屬性的方法

    React styled-components設(shè)置組件屬性的方法

    這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)

    React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)

    這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié),需本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,要的朋友可以參考下
    2021-08-08
  • React中的useMemo 和 useEffect 執(zhí)行順序

    React中的useMemo 和 useEffect 執(zhí)行順序

    在 React 組件的渲染過程中,useMemo 和 useEffect 的執(zhí)行順序是不同的,本文給大家介紹React中的useMemo 和 useEffect 哪個先執(zhí)行,感興趣的朋友一起看看吧
    2025-01-01
  • react導(dǎo)出excel文件的四種方式

    react導(dǎo)出excel文件的四種方式

    本文主要介紹了react導(dǎo)出excel文件的四種方式,主要包括原生js導(dǎo)出,使用?js-export-excel,使用xlsx導(dǎo)出, 使用react-html-table-to-excel,感興趣的可以了解一下
    2023-11-11

最新評論

绥宁县| 苗栗市| 清水县| 洱源县| 阿勒泰市| 克什克腾旗| 广安市| 昌宁县| 抚松县| 湖口县| 沂水县| 徐闻县| 双牌县| 巴青县| 双流县| 上饶市| 波密县| 新泰市| 鄂州市| 广灵县| 莱西市| 宁乡县| 栾川县| 麻城市| 镇宁| 岢岚县| 贵定县| 神木县| 内丘县| 涿鹿县| 大方县| 冷水江市| 盘山县| 泽库县| 乐至县| 福鼎市| 高安市| 花莲县| 锦州市| 堆龙德庆县| 名山县|