Logo

dev-resources.site

for different kinds of informations.

Reusing state management: HOC vs Hook

Published at
5/26/2024
Categories
react
state
modularity
patterns
Author
buchananwill
Categories
4 categories in total
react
open
state
open
modularity
open
patterns
open
Author
12 person written this
buchananwill
open
Reusing state management: HOC vs Hook

I've been looking at alternative approaches to HoC wrappers, as the evolution of client/server components and React best practices has pushed the HoC pattern down the list of "good modularity patterns". As a comparative exercise, I took this wrapper, and re-wrote it as a hook, using useCallback and React.memo for re-render stability. The hook version seems more in line with modern React style, but otherwise doesn't seem to offer much over the HoC version. Has anyone else looked at these patterns, or otherwise have a better solution?

HoC:

"use client";

import { DtoUiComponent, Entity } from "../../types";
import React from "react";
import { useDtoStoreDispatchAndListener } from "../../hooks/main";
import { useDtoStoreDelete } from "../../hooks/main";

export function DtoComponentWrapper<T extends Entity>({
  entityClass,
  id,
  uiComponent: UiComponent,
}: {
  entityClass: string;
  id: string | number;
  uiComponent?: DtoUiComponent<T>;
}) {
  const { currentState, dispatchWithoutControl } =
    useDtoStoreDispatchAndListener<T>(
      id,
      entityClass,
      UiComponent?.name || "component",
    );
  const { dispatchDeletion, deleted } = useDtoStoreDelete(entityClass, id);

  return (
    UiComponent && (
      <UiComponent
        entity={currentState}
        entityClass={entityClass}
        dispatchWithoutControl={dispatchWithoutControl}
        deleted={deleted}
        dispatchDeletion={dispatchDeletion}
      />
    )
  );
}
Enter fullscreen mode Exit fullscreen mode

Hook:

"use client";

import { DtoUiComponent, Entity } from "../../types";
import React, { memo, useCallback } from "react";
import {
  useDtoStoreDelete,
  useDtoStoreDispatchAndListener,
} from "../../hooks/main";

export function useDtoComponent<T extends Entity>(
  entityClass: string,
  UiComponent: DtoUiComponent<T>,
) {
  return useCallback(
    memo(({ id }: { id: string | number }) => {
      const { currentState, dispatchWithoutControl } =
        useDtoStoreDispatchAndListener<T>(
          id,
          entityClass,
          UiComponent?.name || "component",
        );
      const { dispatchDeletion, deleted } = useDtoStoreDelete(entityClass, id);

      return (
        UiComponent && (
          <UiComponent
            entity={currentState}
            entityClass={entityClass}
            dispatchWithoutControl={dispatchWithoutControl}
            deleted={deleted}
            dispatchDeletion={dispatchDeletion}
          />
        )
      );
    }),
    [entityClass, UiComponent],
  );
}
Enter fullscreen mode Exit fullscreen mode
state Article's
30 articles in total
Favicon
Svelte 5: Share state between components (for Dummies)
Favicon
Pampanga State Agricultural University
Favicon
Data Flow in LLM Applications: Building Reliable Context Management Systems
Favicon
Props and State in React
Favicon
Radar Market Innovations: Phased Array Solid-State Radar Development
Favicon
A single state for Loading/Success/Error in NgRx
Favicon
Advanced State Management - XState
Favicon
Top 7 Tips for Managing State in JavaScript Applications 🌟
Favicon
MithrilJS component with state management
Favicon
React State Management: When & Where add your states?
Favicon
STATE MANAGEMENT IN REACT
Favicon
State Management with Zustand
Favicon
A practical summary of React State variables & Props!
Favicon
State in React
Favicon
Weak memoization in Javascript
Favicon
Crafting a Global State Hook in React
Favicon
Reusing state management: HOC vs Hook
Favicon
State Vs Prop in React [Tabular Difference]
Favicon
Mastering XState Fundamentals: A React-powered Guide
Favicon
Does limiting state matter on the FrontEnd?
Favicon
Reducer vs. Finite State Machines: Understanding the Paradigm Shift
Favicon
A tool that can be used by anyone to manage React Query state externally
Favicon
Taming the State Beast: Redux vs. Recoil in React
Favicon
11 friends of state management in Angular
Favicon
React State Management
Favicon
How Can State Management Be Applied To A Real World Case-Scenario
Favicon
No more State Management with Signals
Favicon
How to keep state between page refreshes in React
Favicon
How to sync React state across tabs with workers
Favicon
State Management Patterns in React

Featured ones: