Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

godaddy/addhoc

Repository files navigation

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

Motivation

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

Benefits

addhoc creates HOC functions that automatically:

Installation

npm install addhoc

API

/**** Public API ****/// This is the main exported entrypointaddhoc(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 therender function of your top-level component. Your callback is provided a function parameter that returns the wrappedchild that's initially provided to the HOC. You can call that callback with an object ofprops to add to the wrappedcomponent.

Example 1: Adding a prop

importaddhocfrom'addhoc';importMyComponentfrom'./my-component';constwithFooProp=addhoc(getWrappedComponent=>getWrappedComponent({foo:true}),'WithFooProp');constMyComponentWithFoo=withFooProp(MyComponent);// Rendering a MyComponentWithFoo will create a MyComponent with prop foo = true

Example 2: Wrapping in another component

importReactfrom'react';importaddhocfrom'addhoc';importMyComponentfrom'./my-component';constwithDiv=addhoc(getWrappedComponent=><div>{getWrappedComponent()}</div>,'WithDiv');constMyComponentWithDiv=withDiv(MyComponent);// Rendering a MyComponentWithDiv will render a div that wraps a MyComponent

Example 3: React 16 Context consumer

importReactfrom'react';importaddhocfrom'addhoc';importMyComponentfrom'./my-component';constMyContext=React.createContext('DefaultValue');constwithMyContext=addhoc(getWrappedComponent=><MyContext.Consumer>{value=>getWrappedComponent({ value})}</MyContext.Consumer>,'WithMyContext');constMyComponentWithMyContext=withMyContext(MyComponent);// ...render(){return<MyContext.Providervalue='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 thename param toaddhoc and they'll be passed through as additionalparameters to your render function:

importaddhocfrom'addhoc';importMyComponentfrom'./my-component';constwithFooProp=addhoc((getWrappedComponent,extra)=>getWrappedComponent({foo:extra}),'WithFoo','EXTRA');constMyComponentWithFoo=withFooProp(MyComponent);// Rendering a MyComponentWithFoo will get a `foo` prop with value `EXTRA`

Testing

npmtest

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp