亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

react封裝一個(gè)類似安卓的toast控件message

褰辯話 / 3645人閱讀

摘要:項(xiàng)目中經(jīng)常會(huì)用到類似安卓的的控件。封裝一個(gè)這樣的組件是每個(gè)項(xiàng)目必須的事情。封裝完成這樣一個(gè)組件,具體使用方法如下使用方法第一步第二步需要的地方

項(xiàng)目中經(jīng)常會(huì)用到類似安卓的toast的控件。封裝一個(gè)這樣的組件是每個(gè)項(xiàng)目必須的事情。

import React from "react";
import ReactDOM from "react-dom";

let seed = 0;
const now = Date.now();
function getUuid() {
  return `rcNotification_${now}_${seed++}`;
}

class Message extends React.Component {

  constructor(props) {
    super(props);
    this.state = {data: []};
    this.add = this.add.bind(this);
    this.remove = this.remove.bind(this);
  }

  add(notice) {
    console.log("msg123", "add");
    this.setState({data: [...this.state.data, notice]});
    const that = this;
    setTimeout((() => that.remove(notice.key)), 2000);
  }

  remove(key) {
    console.log("msg123", "remove");

    const temp = this.state.data.filter(item => {
      const result = item.key != key;
      return result;
    });
    this.setState({data: [...temp]});
  }

  render() {
    console.log("msg123", this.state.data);
    return (
      
{ this.state.data.map(item => { if (item.type == "success") { return
{item.msg}
; } return
{item.msg}
; }) }
); } } let instance; Message.newInstance = function () { if (instance) { return instance; } const div = document.createElement("div"); document.body.appendChild(div); const message = ReactDOM.render(, div); console.log("msg123", "newInstance"); instance = { success(msg) { console.log("msg123", "success"); message.add({type: "success", msg, key: getUuid()}); }, warning(msg) { console.log("msg123", "warning"); message.add({type: "warning", msg, key: getUuid()}); }, component: message, }; return instance; }; export default Message;

封裝完成這樣一個(gè)組件,具體使用方法如下:

使用方法:
第一步:

componentWillMount() {
    this.message = Message.newInstance();
 }

第二步需要的地方

 this.message.success("xxx");
 this.message.warning("xxx);

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/95582.html

相關(guān)文章

  • [Android] Toast問題深度剖析(二)

    摘要:所以,從體驗(yàn)上考慮,這個(gè)情況并不屬于問題。一般情況下,這個(gè)節(jié)點(diǎn)占據(jù)了除了通知欄的所有區(qū)域。通知給對(duì)象的消息,都會(huì)被這個(gè)內(nèi)部對(duì)象進(jìn)行處理通過執(zhí)行處理消息在通知給對(duì)象顯示的時(shí)候,對(duì)象將給對(duì)象發(fā)送一條消息,并在的函數(shù)中執(zhí)行。 歡迎大家前往云+社區(qū),獲取更多騰訊海量技術(shù)實(shí)踐干貨哦~ 作者:QQ音樂技術(shù)團(tuán)隊(duì) 題記 Toast 作為 Android 系統(tǒng)中最常用的類之一,由于其方便的api設(shè)計(jì)和...

    cloud 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<