New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wordpress/compose

Package Overview
Dependencies
Maintainers
9
Versions
208
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/compose

WordPress higher-order components (HOCs).

  • 3.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104K
decreased by-25.19%
Maintainers
9
Weekly downloads
 
Created
Source

Compose

The compose package is a collection of handy Higher Order Components (HOCs) you can use to wrap your WordPress components and provide some basic features like: state, instance id, pure...

The compose function is an alias to flowRight from Lodash. It comes from functional programming, and allows you to compose any number of functions. You might also think of this as layering functions; compose will execute the last function first, then sequentially move back through the previous functions passing the result of each function upward.

An example that illustrates it for two functions:

const compose = ( f, g ) => x
    => f( g( x ) );

Here's a simplified example of compose in use from Gutenberg's PluginSidebar component:

Using compose:

const applyWithSelect = withSelect( ( select, ownProps ) => {
	return doSomething( select, ownProps);
} );
const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => {
	return doSomethingElse( dispatch, ownProps );
} );

export default compose(
	withPluginContext,
	applyWithSelect,
	applyWithDispatch,
)( PluginSidebarMoreMenuItem );

Without compose, the code would look like this:

const applyWithSelect = withSelect( ( select, ownProps ) => {
	return doSomething( select, ownProps);
} );
const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => {
	return doSomethingElse( dispatch, ownProps );
} );

export default withPluginContext(
	applyWithSelect(
		applyWithDispatch(
			PluginSidebarMoreMenuItem
		)
	)
);

Installation

Install the module

npm install @wordpress/compose --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.

API

For more details, you can refer to each Higher Order Component's README file. Available components are located here.

compose

src/index.js#L22-L22

Composes multiple higher-order components into a single higher-order component. Performs right-to-left function composition, where each successive invocation is supplied the return value of the previous.

Parameters

  • hocs ...Function: The HOC functions to invoke.

Returns

Function: Returns the new composite function.

createHigherOrderComponent

src/index.js#L6-L6

Given a function mapping a component to an enhanced component and modifier name, returns the enhanced component augmented with a generated displayName.

Parameters

  • mapComponentToEnhancedComponent Function: Function mapping component to enhanced component.
  • modifierName string: Seed name from which to generated display name.

Returns

WPComponent: Component class with generated display name assigned.

ifCondition

src/index.js#L7-L7

Higher-order component creator, creating a new component which renders if the given condition is satisfied or with the given optional prop name.

Parameters

  • predicate Function: Function to test condition.

Returns

Function: Higher-order component.

pure

src/index.js#L8-L8

Given a component returns the enhanced component augmented with a component only rerendering when its props/state change

Parameters

  • mapComponentToEnhancedComponent Function: Function mapping component to enhanced component.
  • modifierName string: Seed name from which to generated display name.

Returns

WPComponent: Component class with generated display name assigned.

withGlobalEvents

src/index.js#L9-L9

Undocumented declaration.

withInstanceId

src/index.js#L10-L10

A Higher Order Component used to be provide a unique instance ID by component.

Parameters

  • WrappedComponent WPElement: The wrapped component.

Returns

Component: Component with an instanceId prop.

withSafeTimeout

src/index.js#L11-L11

A higher-order component used to provide and manage delayed function calls that ought to be bound to a component's lifecycle.

Parameters

  • OriginalComponent Component: Component requiring setTimeout

Returns

Component: Wrapped component.

withState

src/index.js#L12-L12

A Higher Order Component used to provide and manage internal component state via props.

Parameters

  • initialState ?Object: Optional initial state of the component.

Returns

Component: Wrapped component.



Code is Poetry.

Keywords

FAQs

Package last updated on 20 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc