zoukankan      html  css  js  c++  java
  • React 方法组件调用

    【Message.tsx】
    import Snackbar from '@material-ui/core/Snackbar'; import Alert from '@material-ui/lab/Alert'; import React from 'react'; export const ShowMessage=(props:any)=>{ const {open,openName,message,onClose,duration,severity} = props; const location=props.location?props.location:{ vertical: 'top', horizontal: 'right' }; const msg = (<div dangerouslySetInnerHTML={{__html:message}} />) return ( <Snackbar open={open} autoHideDuration={duration} anchorOrigin={location} onClose={()=>{onClose(openName)}}> <Alert onClose={()=>{onClose(openName)}} severity={severity}> {msg} </Alert> </Snackbar> ) }

     【使用】

    import React from 'react';
    import {ShowMessage} from "./Message";
    
    type State = {
        msgOpen:boolean
        message:string
    }
    class Example extends React.Component<{}, State> {
      constructor(props: {} | Readonly<{}>) {
            super(props);
    
            this.state = {
                msgOpen:false,        
                message:"",
            }
    
      private setStateVal(key: string, value: any) {
            let state:any = {};
            state[key] = value
            this.setState(state)
        }
       render() {
            return (
             <ShowMessage 
                          open={this.state.msgOpen}
                          openName='msgOpen'
                          message="操作成功"
                          onClose={(name:string)=>{this.setStateVal(name,false)}}
                          duration={2000}
                          severity="success"  
                        />
    
    
        )}
    
    
    }
    

      

     

  • 相关阅读:
    面试题总结
    h5c3新特性
    redis常用命令大全
    windows下挂载linux的nfs网络硬盘
    mysql之char、varchar、text对比
    Lua与C的交互
    通信模型socket
    程序编译流程
    区块链共识机制(POW、POS、DPOS等)的优缺点
    .net c#获取自定义Attribute
  • 原文地址:https://www.cnblogs.com/boye169/p/15567632.html
Copyright © 2011-2022 走看看