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

hook-into-props

v4.0.1

Published

Tiny abstraction to use React hooks with class components.

Readme

🚢 hookIntoProps

Introduction

class DisplayWindowSize extends React.Component {
  render() {
    this.props.windowSize
  }
}

const useHooks = () => ({ windowSize: useWindowSize() })

export default hookIntoProps(useHooks)(DisplayWindowSize)

Installation

You can install it via npm i hook-into-props. (200 bytes + 1kb dependencies. See bundle-phobia)

Alternatively you can copy the source code, it's only a few lines.

Examples

Using props with hooks

class SearchResults extends React.Component {
  render() {
    this.props.isLoading ? 'loading' : this.props.searchResults
  }
}

const useHooks = props => {
  const [isLoading, searchResults] = useFetch(
    `https://foo.com/?search=${props.searchTerm}`
  )

  return { isLoading, searchResults }
}

export default hookIntoProps(useHooks)(WindowDetails)

Using multiple hooks

class WindowDetails extends React.Component {
  render() {
    this.props.windowSize + ' ' + this.props.scrollPosition
  }
}

const useHooks = () => {
  const windowSize = useWindowSize()
  const scrollPosition = useWindowScrollPosition()

  return { windowSize, scrollPosition }
}

export default hookIntoProps(useHooks)(WindowDetails)

Reusable Higher-Order-Components

const useHooks = () => ({ windowSize: useWindowSize() })

export const withWindowSize = hookIntoProps(useHooks)

Replacing the old context API

/* Old, slow context api 😢 */

import React from 'react'

class UserDetails extends React.Component {
  render() {
    const { referralCode, timezoneOffset, featureList } = this.context
    // ...
  }
}

UserDetails.contextTypes = {
  referralCode: PropTypes.string,
  timezoneOffset: PropTypes.number,
  featureList: PropTypes.arrayOf(PropTypes.string)
}

export default UserDetails
/* refactored to the new context API ✨ */

import React from 'react'
import { ReferralCode, TimezoneOffset, FeatureList } from '~/contexts'

class UserDetails extends React.Component {
  render() {
    const { referralCode, timezoneOffset, featureList } = this.props
    // ...
  }
}

const useHooks = () => ({
  referralCode: useContext(ReferralCode),
  timezoneOffset: useContext(TimezoneOffset),
  featureList: useContext(FeatureList)
})

export default hookIntoProps(useHooks)(DisplayWindowSize)

Under the hood

// That's the basic source code:
const hookIntoProps = useHooks => Component => {
  const HooksProvider = props => <Component {...props} {...useHooks(props)} />

  return HooksProvider
}

The useHooks argument should be a custom hook. It can be that used to call, format and combine React hooks. We can use it to return an object which is spread to the props of the passed component. HookProvider is a functional Higher-Order-Component. It allows us to call react hooks & add aditional props to our component.

(The fact that the hook calls in useHooks are only executed when useHooks is called is known as lazy evaluation. A function used to inject code into another component is also known as a Thunk.)

Alternatives

Render-props

We could also create a simple Component that allows us to use consume hooks through render-props:

const HookProvider = ({ children, useHooks }) => children(useHooks())

class DisplayWindowSize extends React.Component {
  render() {
    return (
      <HookProvider useHooks={() => useWindowSize()}>
        {windowSize => <span>{windowSize}</span>}
      </HookProvider>
    )
  }
}

While this looks clean, I'm not sure it's really all that useful. Here, HookProvider must be called in the render method, meaning we won't have access to the hook result in our class methods. At that point, we could also write a functional component and call hooks directly.

withReactHooks

withReactHooks is a HoC that allows you to call hooks directly in a class component's render method. I would advise against this approach as well.

Not only does it suffer from the same caveats as the render-props approach, but it relies on mutating the passed Component's render method. The React documentation explicitly warns against this. Instead of mutation, composition is recommended, which is the approach taken by hookIntoProps.