加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react+mobx 编写 withStoreHistory 装饰器

发布时间:2020-12-15 20:39:07 所属栏目:百科 来源:网络整理
导读:主要作用是向store里面注入一个history对象,方便story里面的函数调用 function withStoreHistory(storeName) { if (!storeName) return console.error(`必须输入一个查询数据的store`); return function(Target) { class WithStoreHistory extends Componen

主要作用是向store里面注入一个history对象,方便story里面的函数调用

function withStoreHistory(storeName) {
  if (!storeName) return console.error(`必须输入一个查询数据的store`);
  return function(Target) {
    class WithStoreHistory extends Component {
      componentDidMount() {
        const { history } = this.props;
        const store = this.props[storeName];
        store.history = history;
      }
      render() {
        return <Target {...this.props} />;
      }
    }
    return WithStoreHistory;
  };
}

使用

需要在inject调用后才能获取到store的数据,所以写在inject下面

const MERCHANTSTORE = "merchantStore";

@inject(MERCHANTSTORE)
@withStoreHistory(MERCHANTSTORE)
@observer
class BusinessEntrance extends Component {
  render() {
    return (
      <div>...</div>
    );
  }
}

函数中使用

@action.bound
  handleSettingData() {
    this.history.push("/merchants_settled");
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读