Skip to content

How to Fix React Higher-Order Component Issues

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to create HOCs that preserve display names statics and refs. This matters because HOCs lose original component display name and static methods. In real-world applications, this pattern appears in code reuse in auth logging and data fetching HOCs.

The Wrong Way

function withLogger(Wrapped) {
  return class extends React.Component {
    render() { return <Wrapped {...this.props} />; }
  };
}
function Button(props) { return <button {...props} />; }
Button.someStatic = 'value';
const LoggedBtn = withLogger(Button);
LoggedBtn.displayName = '' (anonymous) static lost

The Right Way

import hoistNonReactStatics from 'hoist-non-react-statics';
function withLogger(Wrapped) {
  class WithLogger extends React.Component {
    render() { return <Wrapped {...this.props} />; }
  }
  WithLogger.displayName = 'withLogger(' + (Wrapped.displayName || Wrapped.name) + ')';
  hoistNonReactStatics(WithLogger, Wrapped);
  return React.forwardRef((props, ref) => <WithLogger {...props} forwardedRef={ref} />);
}
displayName = 'withLogger(Button)' static preserved

Prevention

  • Set displayName on HOC wrapper
  • Use hoist-non-react-statics for static methods
  • Use forwardRef in HOCs for ref access
  • Consider hooks as simpler HOC alternative

FAQ

### What is the difference between HOCs and hooks??

HOCs wrap and return new components. Hooks reuse stateful logic inside components. Hooks are preferred in modern React avoiding wrapper hell.

How do HOCs work with memo??

React.memo prevents re-renders. If HOC wraps memoized component, HOC controls re-render timing. Apply memo on inner component.

Can I chain multiple HOCs??

Yes but each must use hoistNonReactStatics. Order matters -- innermost wraps the actual component.

This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro