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 🙏

© 2024 – Pkg Stats / Ryan Hefner

addhoc

v2.1.0

Published

Handy little helper to create proper HOC functions complete with hoisted statics and forwarded refs

Downloads

166

Readme

addhoc

Handy little helper to create proper HOC functions complete with hoisted statics and forwarded refs

Motivation

As defined in the React documentation, a Higher Order Component, or HOC, is a function that returns a React component that wraps a specified child component and often provides augmented functionality. Implementing HOCs can be hard, especially when considering hoisting statics, managing ref forwarding, and handling display name. addhoc aims to handle these challenges for you.

Benefits

addhoc creates HOC functions that automatically:

Installation

npm install addhoc

API

/**** Public API ****/
// This is the main exported entrypoint
addhoc(renderFn: Function, [name: String = 'WithHOC'], [...extraArgs]): Function

/**** Signatures, not exported API ****/
// This is the signature of the renderFn parameter to addhoc()
renderFn(getWrappedComponent: Function, [...extraArgs]): React.Component

// This is the signature of the getWrappedComponent parameter to renderFn()
getWrappedComponent([additionalProps: Object]): React.Component

Usage

addhoc is a function that returns a HOC function. To construct your HOC, you simply pass a callback that acts as the render function of your top-level component. Your callback is provided a function parameter that returns the wrapped child that's initially provided to the HOC. You can call that callback with an object of props to add to the wrapped component.

Example 1: Adding a prop

import addhoc from 'addhoc';
import MyComponent from './my-component';

const withFooProp = addhoc(getWrappedComponent => getWrappedComponent({ foo: true }), 'WithFooProp');
const MyComponentWithFoo = withFooProp(MyComponent);
// Rendering a MyComponentWithFoo will create a MyComponent with prop foo = true

Example 2: Wrapping in another component

import React from 'react';
import addhoc from 'addhoc';
import MyComponent from './my-component';

const withDiv = addhoc(getWrappedComponent =>
  <div>
    { getWrappedComponent() }
  </div>, 'WithDiv');
const MyComponentWithDiv = withDiv(MyComponent);
// Rendering a MyComponentWithDiv will render a div that wraps a MyComponent

Example 3: React 16 Context consumer

import React from 'react';
import addhoc from 'addhoc';
import MyComponent from './my-component';

const MyContext = React.createContext('DefaultValue');
const withMyContext = addhoc(getWrappedComponent =>
  <MyContext.Consumer>
    { value => getWrappedComponent({ value }) }
  </MyContext.Consumer>, 'WithMyContext');
const MyComponentWithMyContext = withMyContext(MyComponent);

// ...
render() {
  return <MyContext.Provider value='ProvidedValue'>
    <MyComponentWithMyContext />
  </MyContext.Provider>
}

// Now, the MyComponentWithMyContext automatically gets a prop called `value` that gets the context value passed in from
// the context.

Example 4: Passing through configuration

Sometimes, you want to set some values as part of assembling the HOC and have those available in your render function. You can pass arbitrary parameters after the name param to addhoc and they'll be passed through as additional parameters to your render function:

import addhoc from 'addhoc';
import MyComponent from './my-component';

const withFooProp = addhoc((getWrappedComponent, extra) => getWrappedComponent({ foo: extra }), 'WithFoo', 'EXTRA');
const MyComponentWithFoo = withFooProp(MyComponent);
// Rendering a MyComponentWithFoo will get a `foo` prop with value `EXTRA`

Testing

npm test