Git Product home page Git Product logo

addhoc's Introduction

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

addhoc's People

Contributors

3rd-eden avatar agerard-godaddy avatar decompil3d avatar dependabot[bot] avatar greenkeeper[bot] avatar ifedaviid avatar rxmarbles avatar shawnkoon avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

addhoc's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

An in-range update of eslint-plugin-jsx-a11y is breaking the build 🚨

The devDependency eslint-plugin-jsx-a11y was updated from 6.2.1 to 6.2.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-plugin-jsx-a11y is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Commits

The new version differs by 102 commits.

  • 057fb27 6.2.2
  • fc56208 Merge pull request #615 from evcohen/changelog--v6.2.2
  • 8c5f964 Changelog for v6.2.2
  • f82fbcb Merge pull request #614 from evcohen/update--jsx-ast-utils
  • 1c3e63a Update jsx-ast-utils to v2.2.1
  • c571293 Merge pull request #613 from evcohen/add-babel-to-dev-deps
  • c398d3a Add @babel/cli to the dev dependencies
  • 13b370c Merge pull request #610 from evcohen/greenkeeper/flow-bin-0.102.0
  • e28b81a Merge branch 'master' into greenkeeper/flow-bin-0.102.0
  • e3163e3 Merge pull request #603 from evcohen/another-attempt-at-eslint-v6
  • f121a78 Merge branch 'master' into another-attempt-at-eslint-v6
  • f3de162 Merge pull request #611 from evcohen/update-jsx-ast-utils
  • 91f17be Update ESLint to v6
  • 1eb9f19 Update jsx-ast-utils to 2.2.0
  • 313bc03 chore(package): update flow-bin to version 0.102.0

There are 102 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.