React-native中的彈窗提示組件

開(kāi)發(fā)中遇到需要在網(wǎng)絡(luò)請(qǐng)求錯(cuò)誤的時(shí)候提供一個(gè)統(tǒng)一的界面,結(jié)合Redux和一個(gè)封裝好的組件,很好處理.這個(gè)組件在iReading和reddit的app里面都使用過(guò),使用比較簡(jiǎn)單
直接參照ireading代碼

github:react-native-root-toast

安裝 需要這個(gè)組件

在./app/utils/ToastUtils.js文件中定義了方法

import {
  Platform
} from 'react-native';
import Toast from 'react-native-root-toast';//導(dǎo)入組件

let toast;
//短時(shí)間提示的方法
export const toastShort = (content) => {
  if (toast !== undefined) {
    Toast.hide(toast);
  }
  toast = Toast.show(content.toString(), {
    duration: Toast.durations.SHORT,
    position: Platform.OS === 'android' ? Toast.positions.BOTTOM : Toast.positions.CENTER,
    shadow: true,
    animation: true,
    hideOnPress: true,
    delay: 0
  });
};
//長(zhǎng)時(shí)間提示的方法
export const toastLong = (content) => {
  if (toast !== undefined) {
    Toast.hide(toast);
  }
  toast = Toast.show(content.toString(), {
    duration: Toast.durations.LONG,
    position: Platform.OS === 'android' ? Toast.positions.BOTTOM : Toast.positions.CENTER,
    shadow: true,
    animation: true,
    hideOnPress: true,
    delay: 0
  });
};

由于ireading的最新版改為使用redux-saga的中間件來(lái)處理具體的邏輯,所以網(wǎng)絡(luò)請(qǐng)求處理的問(wèn)題也在saga里.如果沒(méi)有使用redux-saga這部分放到actions里做也是可以的,reddit好像是這么搞的.或者你可以看看ireading的以前使用redux-thunk的版本.

./app/sagas/read.js

 import { toastShort } from '../utils/ToastUtil';//導(dǎo)入toast
import { request } from '../utils/RequestUtil';
import { WEXIN_ARTICLE_LIST } from '../constants/Urls';
import { fetchArticleList, receiveArticleList } from '../actions/read';

export function* requestArticleList(isRefreshing, loading, typeId, isLoadMore, page) {
  try {
    yield put(fetchArticleList(isRefreshing, loading, isLoadMore));
    const articleList = yield call(request,
      `${WEXIN_ARTICLE_LIST}?typeId=${typeId}&page=${page}`,
      'get');
    yield put(receiveArticleList(articleList.showapi_res_body.pagebean.contentlist, typeId));
    const errorMessage = articleList.showapi_res_error;
    if (errorMessage && errorMessage !== '') {
      yield toastShort(errorMessage); //錯(cuò)誤處理
    }
  } catch (error) {
    yield put(receiveArticleList([], typeId));
    toastShort('網(wǎng)絡(luò)發(fā)生錯(cuò)誤,請(qǐng)重試');//還是錯(cuò)誤處理
  }
}

效果嘛,看組件 github的圖片

Toast圖片

圖太大,gif還不知道怎么改大小.

結(jié)束

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 持續(xù)更新中...... 一套企業(yè)級(jí)的 UI 設(shè)計(jì)語(yǔ)言和 React 實(shí)現(xiàn)。 https://mobile.ant....
    日不落000閱讀 6,067評(píng)論 0 35
  • React Native優(yōu)秀博客,以及優(yōu)秀的Github庫(kù)列表(很多英文資料源自于[awesome-react-n...
    董董董董董董董董董大笨蛋閱讀 11,010評(píng)論 4 162
  • 簡(jiǎn)短說(shuō)明 收錄一些好用的RN第三方組件,以方便日常的使用,大家有什么推薦的也可以跟我說(shuō),我加進(jìn)去。如有冒犯,可以聯(lián)...
    以德扶人閱讀 43,906評(píng)論 44 214
  • 1. 常用的塊元素有 ... 2. 常用的內(nèi)聯(lián)元素有 3. 常用的內(nèi)聯(lián)塊狀元素有
    李凱強(qiáng)閱讀 223評(píng)論 0 1
  • ——寫(xiě)在前面 常在想,愛(ài)情是什么?是舉案齊眉的白頭偕老?還是化蝶般的千古絕唱?或是偶爾翻開(kāi)記憶閘門(mén)的那屢乍開(kāi)的情竇...
    周延龍閱讀 616評(píng)論 3 13

友情鏈接更多精彩內(nèi)容