npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@diginet/class-hooks

v2.0.4

Published

Use React hooks in class components with field bindings

Readme

@diginet/class-hooks

Use React hooks in class components — cleanly, type-safely, and without HOCs or boilerplate.

This library lets you bind React hooks to class component fields, giving you the power of hooks while preserving an OOP-style structure.

React hooks revolutionized component logic by promoting composition over inheritance. But they also made class components feel obsolete — especially when trying to reuse stateful logic.

@diginet/class-hooks brings the best of both worlds:

  • It lets you use modern React hooks inside class components.
  • It gives each hook a clean, typed field on the class.
  • It avoids messy patterns like higher-order components or verbose lifecycle glue.

Powerful, composable logic without sacrificing inheritance when needed.

Features

  • Access hook results directly as class instance fields
  • Works with multiple hooks
  • Full TypeScript support with auto-inferred types
  • ESM-first, with CommonJS fallback
  • No HOCs, decorators, or boilerplate required

Installation

npm install @diginet/class-hooks

Usage

import React from 'react';
import { classHooks } from '@diginet/class-hooks';
import { useWindowWidth, useCurrentTime } from 'someHooks.js'

const hooks = {
  windowWidth: useWindowWidth,
  currentTime: useCurrentTime,
};

class MainDisplay extends classHooks(React.Component, hooks) {
  render() {
    return (
      <div>
        {this.renderClassHooks()}
        <h1>Width: {this.windowWidth}px</h1>
        <h2>Time: {this.currentTime}</h2>
      </div>
    );
  }
}

How it works

Internally, your hooks are rendered as hidden bridge components. Their return values are assigned to class fields (matching the hook names you provide).

Use this.renderClassHooks() once in your render tree to activate the internal sync.

API

classHooks(BaseComponent, hooks)

Wraps your class to inject the defined hooks. Parameters:

  • BaseComponent: usually React.Component or React.PureComponent. Can also be any existing React component class that you want to add hooks to.
  • hooks: an object where each key maps to a hook function

Returns an extended class that:

  • Has a field for each hook (e.g., this.windowWidth)
  • Includes a renderClassHooks() method to include in your render tree
const hooks = {
  count: useCounter,
};

class Counter extends classHooks(React.Component, hooks) {
  render() {
    return (
      <>
        {this.renderClassHooks()}
        <p>{this.count}</p>
      </>
    );
  }
}

License

MIT