Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

recompose

Package Overview
Dependencies
Maintainers
2
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recompose

A React utility belt for function components and higher-order components

  • 0.23.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
287K
decreased by-60.84%
Maintainers
2
Weekly downloads
 
Created

What is recompose?

Recompose is a React utility belt for function components and higher-order components (HOCs). It provides a set of helper functions to simplify the process of writing and managing HOCs, making it easier to enhance and compose components in a more declarative and functional style.

What are recompose's main functionalities?

withState

The `withState` HOC adds state to a functional component. In this example, it adds a `counter` state and a `setCounter` function to the `Counter` component.

const enhance = withState('counter', 'setCounter', 0);
const Counter = enhance(({ counter, setCounter }) => (
  <div>
    <p>{counter}</p>
    <button onClick={() => setCounter(counter + 1)}>Increment</button>
  </div>
));

withHandlers

The `withHandlers` HOC allows you to create handler functions that can be passed as props to the component. In this example, it creates an `onClick` handler that increments the `counter` state.

const enhance = withHandlers({
  onClick: ({ counter, setCounter }) => () => setCounter(counter + 1)
});
const Counter = enhance(({ counter, onClick }) => (
  <div>
    <p>{counter}</p>
    <button onClick={onClick}>Increment</button>
  </div>
));

compose

The `compose` function allows you to combine multiple HOCs into a single HOC. In this example, it combines `withState` and `withHandlers` to create an enhanced `Counter` component.

const enhance = compose(
  withState('counter', 'setCounter', 0),
  withHandlers({
    onClick: ({ counter, setCounter }) => () => setCounter(counter + 1)
  })
);
const Counter = enhance(({ counter, onClick }) => (
  <div>
    <p>{counter}</p>
    <button onClick={onClick}>Increment</button>
  </div>
));

branch

The `branch` HOC conditionally applies an HOC based on a predicate function. In this example, it renders a loading component if `isLoading` is true, otherwise it renders `MyComponent`.

const enhance = branch(
  ({ isLoading }) => isLoading,
  renderComponent(() => <div>Loading...</div>)
);
const MyComponent = enhance(({ data }) => (
  <div>{data}</div>
));

renderNothing

The `renderNothing` HOC renders nothing if the predicate function returns true. In this example, it renders nothing if `shouldRender` is false.

const enhance = branch(
  ({ shouldRender }) => !shouldRender,
  renderNothing
);
const MyComponent = enhance(({ data }) => (
  <div>{data}</div>
));

Other packages similar to recompose

Keywords

FAQs

Package last updated on 11 May 2017

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