我们正在尝试自定义 HOC 的参数(特别是react-graphql)。由于这个特定库 HOC 的工作方式,我们无法在调用 HOC 后影响组件状态(查询和选项)。
在像connect()Redux这样的传统 HOC 工厂中,所有包含的逻辑都会立即应用——这对我们来说还为时过早。相反,我将原始 HOC ( applyOriginalHOC()) 的应用推迟到从该组件构建第一个实例之前。
export function applyLazyHOC(args) {
return function wrap(WrappedComponent) {
let ComponentWithLazyHOC; // Closed-over value is scoped to component
class LazyHOC extends Component {
constructor() {
super();
// Lazy initialising of HOC to give customisations an opportunity to be registered
if (!ComponentWithLazyHOC) {
ComponentWithLazyHOC = applyOriginalHOC(
// Adds customisations which have been registered by third party code
myCustomization(args)
)(WrappedComponent);
}
}
render() { …Run Code Online (Sandbox Code Playgroud)