首页 > 其他 > 详细

react+mobx 编写 withStoreHistory 装饰器

时间:2018-12-09 11:06:37      阅读:229      评论:0      收藏:0      [点我收藏+]

主要作用是向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");
  }

react+mobx 编写 withStoreHistory 装饰器

原文:https://www.cnblogs.com/ajanuw/p/10090243.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!