国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

React Native實(shí)現(xiàn)Toast輕提示和loading

這篇具有很好參考價(jià)值的文章主要介紹了React Native實(shí)現(xiàn)Toast輕提示和loading。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

React Native 封裝Toast

前言

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

實(shí)現(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,
  };
}

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

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

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)用即可文章來源地址http://www.zghlxwxcb.cn/news/detail-684995.html

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實(shí)現(xiàn)Toast輕提示和loading的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • uniapp中uview組件庫Toast 消息提示 的使用方法

    uniapp中uview組件庫Toast 消息提示 的使用方法

    目錄 #基本使用 #配置toast主題 #toast結(jié)束跳轉(zhuǎn)URL #API #Props #Params #Methods 此組件表現(xiàn)形式類似uni的 uni.showToast API,但也有不同的地方,具體表現(xiàn)在: uView的 toast 有5種主題可選 可以配置toast結(jié)束后,跳轉(zhuǎn)相應(yīng)URL 目前沒有加載中的狀態(tài),請用uni的 uni.showLoading ,這個需求uni已經(jīng)做得

    2024年01月20日
    瀏覽(48)
  • 【Vant4】Vant4 樣式不顯示問題 && Toast 輕提示不顯示 && Notify 消息提示不顯示

    【Vant4】Vant4 樣式不顯示問題 && Toast 輕提示不顯示 && Notify 消息提示不顯示

    使用 Toast 輕提示 和 Notify 消息提示 時沒有樣式,如下圖 引入所需的樣式,例如: 我要使用 消息提示 (Notify),就引入 import \\\'vant/es/notify/style\\\' 我要使用 輕提示 (Toast),就引入 import \\\'vant/es/toast/style\\\' HomeView.vue End 2023/3/7 一改 【Vue3】vue3的keepAlive保存滾動位置 CKEditor5 添加代碼高亮

    2023年04月18日
    瀏覽(20)
  • vue自定義全局彈出詢問框、輸入框、提示框、toast(附源碼)

    vue自定義全局彈出詢問框、輸入框、提示框、toast(附源碼)

    早前寫過一篇關(guān)于vue自定義彈出詢問框、輸入框、提示框的貼子,當(dāng)時只是實(shí)現(xiàn)了組件化,組件需要在各個業(yè)務(wù)模板進(jìn)行引用,不能全局化使用,不太方便,本次將其改進(jìn)成了全局使用,具體的業(yè)務(wù)模塊不需要引入組件,直接調(diào)用main.js注冊的全局方法即可。 涉及技術(shù)點(diǎn): 遮

    2024年02月03日
    瀏覽(17)
  • 【微信小程序創(chuàng)作之路】- 小程序事件綁定、動態(tài)提示Toast、對話框 Modal

    【微信小程序創(chuàng)作之路】- 小程序事件綁定、動態(tài)提示Toast、對話框 Modal

    第六章 小程序事件綁定、動態(tài)提示Toast、對話框 Modal 本章主要講解小程序事件綁定、動態(tài)提示Toast、對話框 Modal,結(jié)合代碼示例,我們來研究一下! 事件是 視圖層到邏輯層的通訊方式 。事件是小程序和用戶互動的主要方式,通過事件將用戶在 視圖層 的行為,反饋到 邏輯層

    2024年02月14日
    瀏覽(32)
  • react native 0.73 配置 react-native-fs

    npm yarn android/settings.gradle android/app/build.gradle androidappsrcmainjavacomreactnative_demoMainApplication.kt 把原代碼 改為

    2024年04月08日
    瀏覽(75)
  • 探索React Native的世界:gorhom/react-native-animated-tabbar

    項(xiàng)目地址:https://gitcode.com/gorhom/react-native-animated-tabbar 在移動應(yīng)用開發(fā)領(lǐng)域,React Native以其高效、跨平臺的能力受到了廣泛的歡迎。今天,我們要向您推薦一個極具創(chuàng)意且實(shí)用的React Native組件庫——gorhom/react-native-animated-tabbar。它是一個精美設(shè)計(jì)的動畫TabBar,為您的應(yīng)用提供生

    2024年04月17日
    瀏覽(24)
  • React與React Native的異同

    開發(fā)React一段時間了,一直沒有搞清楚React和React Native的差異。今天特意去了解下,發(fā)現(xiàn)差異還真不??! 相同點(diǎn): 1.都是Facebook公司推出的框架。 2.都是基于JSX語言開發(fā)的。 差異點(diǎn): 1、作用的平臺不同. 2、工作原理不同. 3、渲染周期不同. 4、組件構(gòu)成形式不同. 5、宿主平臺的

    2024年02月06日
    瀏覽(24)
  • 維護(hù)積極的react native,為什么會有人造謠react native不維護(hù)了,停止維護(hù)。

    維護(hù)積極的react native,為什么會有人造謠react native不維護(hù)了,停止維護(hù)。

    ? ? ? ? 其實(shí)近幾年我一直關(guān)注react -native,他一直更新頻繁,0.60大重構(gòu),升級了js執(zhí)行引擎Hermes,當(dāng)前已經(jīng)0.70.4版本了。性能越來越提高,但是總感覺到有人在刷百度,只要輸入react-native后面就自動提示熱搜“react-native 停止維護(hù)”,這誤導(dǎo)很多人以為真的不維

    2024年02月11日
    瀏覽(29)
  • 工欲善其事,必先利其器之—react-native-debugger調(diào)試react native應(yīng)用

    工欲善其事,必先利其器之—react-native-debugger調(diào)試react native應(yīng)用

    調(diào)試react應(yīng)用通常利用chrome的inspector的功能和兩個最常用的擴(kuò)展 1、React Developer Tools (主要用于debug組件結(jié)構(gòu)) 2、Redux DevTools (主要用于debug redux store的數(shù)據(jù)) 對于react native應(yīng)用,我們一般就使用react-native-debugger了,它是一個獨(dú)立的應(yīng)用,需要單獨(dú)安裝,在mac下可以用如下命令

    2024年02月16日
    瀏覽(48)
  • RN(React Native)

    RN(React Native)

    ?RN是React的原生組件庫 網(wǎng)址:React Native 中文網(wǎng) · 使用React來編寫原生應(yīng)用的框架 一、 ReactNative開發(fā)環(huán)境的搭建 ? 在本機(jī)搭建RN項(xiàng)目開發(fā)運(yùn)行環(huán)境 ?—— 3GB+ ?①安裝必需的編譯軟件:Node.js + JDK ?②安裝Android原生App開發(fā)軟件 —— Android?Studio ?③使用Android開發(fā)軟件下載編譯必

    2024年02月07日
    瀏覽(36)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包