【问题标题】:how to call a wrapper's method in react如何在反应中调用包装器的方法
【发布时间】:2017-04-11 10:57:32
【问题描述】:

我正在使用 hoc 来包装组件。这个 hoc 在卸载时调度一个动作。为此,它调用了一个“resetState”方法。但是,如果我想在包装组件的另一个位置调用相同的 resetState() 怎么办。 this.resetState 不起作用(逻辑上),我只能想象将函数作为道具传递给包装器。

const resetAtUnmount = function (type) {
  // return the function to be called by redux 'connect'
  return function decorate(WrappedComponent) {
    // return the final class
    return class extends React.Component {
      resetState() {
        store.dispatch({ type });
      }

      componentWillUnmount() {
        this.resetState();
      }

      render() {
        return <WrappedComponent {...this.props} />;
      }
    };
  };
};

导出默认resetAtUnmount;

【问题讨论】:

  • 使用action调度resetState

标签: reactjs components mixins


【解决方案1】:

听起来您回答了自己的问题。将 resetState 作为 prop 传递:&lt;WrappedComponent {...this.props} resetState={this.resetState} /&gt;

【讨论】:

  • 如何从被包装的组件(子组件)调用resetState方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 2021-07-13
  • 2020-09-08
  • 1970-01-01
相关资源
最近更新 更多