您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用react實現(xiàn)消息顯示器”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么用react實現(xiàn)消息顯示器”文章能幫助大家解決問題。
效果
完整代碼:
import React from 'react'; import styles from './styles.less'; import badgeImg from '@/assets/leftmenu/badgeImg.png'; import router from 'umi/router'; import { connect } from 'dva'; import { Popover, Badge, Button, Modal } from 'antd'; function mapStateToProps({ InformationModel }) { return { InformationModel: InformationModel, }; } @connect(mapStateToProps) class Information extends React.Component { constructor(props) { super(props); this.state = { visible: false, unreadList: [], infoTitle: '', infoContent:'', }; } //渲染前調(diào)用 componentWillMount() { } //渲染后調(diào)用 componentDidMount() { this.getunreadDatas(); } //調(diào)用接口獲取未讀數(shù)據(jù) getunreadDatas() { let { dispatch } = this.props; let userid = localStorage.getItem('userid'); let params = { id: userid, pageNum: 0, pageSize: 0 } dispatch({ type: 'InformationModel/getunreadData', payload: { params: params, callback: this.unreadCallback.bind(this) } }); } //接口回調(diào)方法 unreadCallback(e) { this.setState({ unreadList:e }) } //查看詳情 showInfo(e) { let { dispatch } = this.props; let userid = localStorage.getItem('userid'); let params = { id:e.id, userId:userid, } //調(diào)用接口標(biāo)記已讀 dispatch({ type: 'InformationModel/getreadData', payload: { params: params, callback: this.readCallback.bind(this) } }); this.setState({ infoTitle:e.name, infoContent:e.text }) } //標(biāo)記接口回調(diào)函數(shù) readCallback(e){ this.setState({ visible: true, }); //刷新列表 this.getunreadDatas(); } //顯示全部 showAllInfo(){ router.push({ pathname: `/cs/InformationMoreList`, query: { }, }); } //彈框確認(rèn)按鈕 handleOk = e => { console.log(e); this.setState({ visible: false, }); }; //彈框取消按鈕 handleCancel = e => { console.log(e); this.setState({ visible: false, }); }; render() { const content = ( <div className={styles.infoTabs}> <div className={styles.infoList}> { this.state.unreadList.map((item,index)=>{ return <div className={styles.infoRow} onClick={this.showInfo.bind(this,item)}> <div className={styles.infoTitle}>{item.name}</div> <div className={styles.infoContent}>{item.text}</div> </div> }) } </div> <div className={styles.showAll}> <Button onClick={this.showAllInfo.bind(this)} className={styles.showAllBtn}>查看全部</Button> </div> </div> ); return ( <React.Fragment> <div className={styles.allBox}> <Popover onMouseEnter={this.getunreadDatas.bind(this)} content={content} title="消息"> <Badge count={this.state.unreadList.length} showZero> <img src={badgeImg}></img> </Badge> </Popover> <Modal title={this.state.infoTitle} visible={this.state.visible} onOk={this.handleOk} onCancel={this.handleCancel} footer={null} > <p>{this.state.infoContent}</p> </Modal> </div> </React.Fragment> ); } } export default Information;
關(guān)于“怎么用react實現(xiàn)消息顯示器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。