react+mobx 编写withMessage 高阶组建
发布时间:2020-12-15 20:39:26 所属栏目:百科 来源:网络整理
导读:监听store中的amsg属性的变化来调用 提示框 withMessage // withComponents/withMessage.jsimport React,{ Component } from "react";import LightTip from "../components/lightTip";import { set } from "mobx";export default function withMessage(store
withMessage// withComponents/withMessage.js import React,{ Component } from "react"; import LightTip from "../components/lightTip"; import { set } from "mobx"; export default function withMessage(storeName,key = "amsg") { if (!storeName) return console.error(`必须输入一个查询数据的store`); return function(Target) { return class WithMessage extends Component { componentWillMount() { let msg = this.props[storeName][key]; if (msg === undefined) { set(this.props[storeName],{ [key]: "",}); } } close = () => { set(this.props[storeName],{ [key]: "",}); }; render() { return ( <> <LightTip msg={this.props[storeName][key]} onClose={this.close} /> <Target {...this.props} /> </> ); } }; }; } 使用import React,{ Component } from "react"; import { observer,inject } from "mobx-react"; import withMessage from "../withComponents/withMessage"; const INDEXSTORE = "indexStore"; @inject(INDEXSTORE) @observer @withMessage(INDEXSTORE) class Test extends Component { render() { const { indexStore } = this.props; return ( <button onClick={() => { indexStore.amsg = "error"; }} > click me </button> ); } } export default Test; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- F?l?e?x?4?+?s?p?r?i?n?g?+?h?i?b?e?r?n?a?t?e?+?B?l?a?z?e
- c# – 测试在不发送邮件的情况下发送电子邮件的方法
- ruby-on-rails – Paperclip&RMagick – PDF和重命名的
- c – 在常量表达式上下文中定义之前嵌套的`constexpr`函数调
- vue.js指令v-for使用及索引获取
- 解码抛出的C异常的参数(0xE06D7363)
- 转载_使用DDD+GDB开发ARM Linux程序
- Vxworks下flash驱动编写流程
- Oracle 首席工程师:设计保守或是 Java 的最大优势
- c# – 从MMC管理单元显示时,表单具有不同的样式