【发布时间】: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