首页 > 解决方案 > 如何在 React 中重新定义组件?

问题描述

react/redux 有一个水疗中心。品牌有一个组成部分。Media 组件与它类似,但数据是从另一个 Ajax 请求中检索的。我正在尝试从 Brands 继承,但由于某种原因,componentDidMount 是从 Brands 启动的。怎么做才对?

class Media extends Brands {
  loadData = async () => {
  alert('sdfdf'); //not displayed
    const { dispatch, dataList, clientId } = this.props;
    if (!dataList && clientId) {
      dispatch(mainLoaderActions.addBlockingRequest("loadMedia"));

      const res = await mediaApi.getBrandsData(clientId);

      dispatch(dataActions.setBrandsList(res));
      dispatch(mainLoaderActions.deleteBlockingRequest("loadMedia"));
    }
  };

  componentDidMount() {
    alert('aaa'); //not displayed
    this.loadData();
  }

  render() {...

标签: reactjsreact-redux

解决方案


React 不支持类继承,因此很困难!请参阅此处的文档:https ://reactjs.org/docs/composition-vs-inheritance.html这里有一个类似问题的答案:componentDidMount method not trigger when using inherited ES6 react class with an example of 'react' way of实现这一目标。


推荐阅读