{"id":13394700,"url":"https://github.com/acdlite/recompose","last_synced_at":"2025-10-05T19:30:16.722Z","repository":{"id":1643080,"uuid":"43797718","full_name":"acdlite/recompose","owner":"acdlite","description":"A React utility belt for function components and higher-order components.","archived":true,"fork":false,"pushed_at":"2022-09-10T03:59:05.000Z","size":1325,"stargazers_count":14747,"open_issues_count":123,"forks_count":1249,"subscribers_count":172,"default_branch":"master","last_synced_at":"2025-01-08T06:14:50.922Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/acdlite.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-07T05:54:51.000Z","updated_at":"2025-01-07T21:22:05.000Z","dependencies_parsed_at":"2022-06-28T18:14:17.480Z","dependency_job_id":null,"html_url":"https://github.com/acdlite/recompose","commit_stats":null,"previous_names":[],"tags_count":61,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acdlite%2Frecompose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acdlite%2Frecompose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acdlite%2Frecompose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acdlite%2Frecompose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acdlite","download_url":"https://codeload.github.com/acdlite/recompose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235432276,"owners_count":18989486,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:28.681Z","updated_at":"2025-10-05T19:30:16.254Z","avatar_url":"https://github.com/acdlite.png","language":"JavaScript","readme":"## A Note from the Author (acdlite, Oct 25 2018):\n\nHi! I created Recompose about three years ago. About a year after that, I joined the React team. Today, we announced a proposal for [*Hooks*](https://reactjs.org/hooks). Hooks solves all the problems I attempted to address with Recompose three years ago, and more on top of that. I will be discontinuing active maintenance of this package (excluding perhaps bugfixes or patches for compatibility with future React releases), and recommending that people use Hooks instead. **Your existing code with Recompose will still work**, just don't expect any new features. Thank you so, so much to [@wuct](https://github.com/wuct) and [@istarkov](https://github.com/istarkov) for their heroic work maintaining Recompose over the last few years.\n\nRead more discussion about this decision [here](https://github.com/acdlite/recompose/issues/756#issuecomment-438674573).\n\n***\n\nRecompose\n-----\n\n[![build status](https://img.shields.io/travis/acdlite/recompose/master.svg?style=flat-square)](https://travis-ci.org/acdlite/recompose)\n[![coverage](https://img.shields.io/codecov/c/github/acdlite/recompose.svg?style=flat-square)](https://codecov.io/github/acdlite/recompose)\n[![code climate](https://img.shields.io/codeclimate/github/acdlite/recompose.svg?style=flat-square)](https://codeclimate.com/github/acdlite/recompose)\n[![npm version](https://img.shields.io/npm/v/recompose.svg?style=flat-square)](https://www.npmjs.com/package/recompose)\n[![npm downloads](https://img.shields.io/npm/dm/recompose.svg?style=flat-square)](https://www.npmjs.com/package/recompose)\n\nRecompose is a React utility belt for function components and higher-order components. Think of it like lodash for React.\n\n[**Full API documentation**](docs/API.md) - Learn about each helper\n\n[**Recompose Base Fiddle**](https://jsfiddle.net/evenchange4/p3vsmrvo/1599/) - Easy way to dive in\n\n```\nnpm install recompose --save\n```\n\n**📺 Watch Andrew's [talk on Recompose at React Europe](https://www.youtube.com/watch?v=zD_judE-bXk).**\n*(Note: Performance optimizations he speaks about have been removed, more info [here](https://github.com/acdlite/recompose/releases/tag/v0.26.0))*\n\n### Related modules\n\n[**recompose-relay**](src/packages/recompose-relay) — Recompose helpers for Relay\n\n## You can use Recompose to...\n\n### ...lift state into functional wrappers\n\nHelpers like `withState()` and `withReducer()` provide a nicer way to express state updates:\n\n```js\nconst enhance = withState('counter', 'setCounter', 0)\nconst Counter = enhance(({ counter, setCounter }) =\u003e\n  \u003cdiv\u003e\n    Count: {counter}\n    \u003cbutton onClick={() =\u003e setCounter(n =\u003e n + 1)}\u003eIncrement\u003c/button\u003e\n    \u003cbutton onClick={() =\u003e setCounter(n =\u003e n - 1)}\u003eDecrement\u003c/button\u003e\n  \u003c/div\u003e\n)\n```\n\nOr with a Redux-style reducer:\n\n```js\nconst counterReducer = (count, action) =\u003e {\n  switch (action.type) {\n  case INCREMENT:\n    return count + 1\n  case DECREMENT:\n    return count - 1\n  default:\n    return count\n  }\n}\n\nconst enhance = withReducer('counter', 'dispatch', counterReducer, 0)\nconst Counter = enhance(({ counter, dispatch }) =\u003e\n  \u003cdiv\u003e\n    Count: {counter}\n    \u003cbutton onClick={() =\u003e dispatch({ type: INCREMENT })}\u003eIncrement\u003c/button\u003e\n    \u003cbutton onClick={() =\u003e dispatch({ type: DECREMENT })}\u003eDecrement\u003c/button\u003e\n  \u003c/div\u003e\n)\n```\n\n### ...perform the most common React patterns\n\nHelpers like `componentFromProp()` and `withContext()` encapsulate common React patterns into a simple functional interface:\n\n```js\nconst enhance = defaultProps({ component: 'button' })\nconst Button = enhance(componentFromProp('component'))\n\n\u003cButton /\u003e // renders \u003cbutton\u003e\n\u003cButton component={Link} /\u003e // renders \u003cLink /\u003e\n```\n\n```js\nconst provide = store =\u003e withContext(\n  { store: PropTypes.object },\n  () =\u003e ({ store })\n)\n\n// Apply to base component\n// Descendants of App have access to context.store\nconst AppWithContext = provide(store)(App)\n```\n\n### ...optimize rendering performance\n\nNo need to write a new class just to implement `shouldComponentUpdate()`. Recompose helpers like `pure()` and `onlyUpdateForKeys()` do this for you:\n\n```js\n// A component that is expensive to render\nconst ExpensiveComponent = ({ propA, propB }) =\u003e {...}\n\n// Optimized version of same component, using shallow comparison of props\n// Same effect as extending React.PureComponent\nconst OptimizedComponent = pure(ExpensiveComponent)\n\n// Even more optimized: only updates if specific prop keys have changed\nconst HyperOptimizedComponent = onlyUpdateForKeys(['propA', 'propB'])(ExpensiveComponent)\n```\n\n### ...interoperate with other libraries\n\nRecompose helpers integrate really nicely with external libraries like Relay, Redux, and RxJS\n\n```js\nconst enhance = compose(\n  // This is a Recompose-friendly version of Relay.createContainer(), provided by recompose-relay\n  createContainer({\n    fragments: {\n      post: () =\u003e Relay.QL`\n        fragment on Post {\n          title,\n          content\n        }\n      `\n    }\n  }),\n  flattenProp('post')\n)\n\nconst Post = enhance(({ title, content }) =\u003e\n  \u003carticle\u003e\n    \u003ch1\u003e{title}\u003c/h1\u003e\n    \u003cdiv\u003e{content}\u003c/div\u003e\n  \u003c/article\u003e\n)\n```\n\n### ...build your own libraries\n\nMany React libraries end up implementing the same utilities over and over again, like `shallowEqual()` and `getDisplayName()`. Recompose provides these utilities for you.\n\n```js\n// Any Recompose module can be imported individually\nimport getDisplayName from 'recompose/getDisplayName'\nConnectedComponent.displayName = `connect(${getDisplayName(BaseComponent)})`\n\n// Or, even better:\nimport wrapDisplayName from 'recompose/wrapDisplayName'\nConnectedComponent.displayName = wrapDisplayName(BaseComponent, 'connect')\n\nimport toClass from 'recompose/toClass'\n// Converts a function component to a class component, e.g. so it can be given\n// a ref. Returns class components as is.\nconst ClassComponent = toClass(FunctionComponent)\n```\n\n### ...and more\n\n## API docs\n\n[Read them here](docs/API.md)\n\n## Flow support\n\n[Read the docs](docs/flow.md)\n\n## Translation\n\n[Traditional Chinese](https://github.com/neighborhood999/recompose)\n\n## Why\n\nForget ES6 classes vs. `createClass()`.\n\nAn idiomatic React application consists mostly of function components.\n\n```js\nconst Greeting = props =\u003e\n  \u003cp\u003e\n    Hello, {props.name}!\n  \u003c/p\u003e\n```\n\nFunction components have several key advantages:\n\n- They help prevent abuse of the `setState()` API, favoring props instead.\n- They encourage the [\"smart\" vs. \"dumb\" component pattern](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0).\n- They encourage code that is more reusable and modular.\n- They discourage giant, complicated components that do too many things.\n- They allow React to make performance optimizations by avoiding unnecessary checks and memory allocations.\n\n(Note that although Recompose encourages the use of function components whenever possible, it works with normal React components as well.)\n\n### Higher-order components made easy\n\nMost of the time when we talk about composition in React, we're talking about composition of components. For example, a `\u003cBlog\u003e` component may be composed of many `\u003cPost\u003e` components, which are composed of many `\u003cComment\u003e` components.\n\nRecompose focuses on another unit of composition: **higher-order components** (HoCs). HoCs are functions that accept a base component and return a new component with additional functionality. They can be used to abstract common tasks into reusable pieces.\n\nRecompose provides a toolkit of helper functions for creating higher-order components.\n\n## [Should I use this? Performance and other concerns](docs/performance.md)\n\n## Usage\n\nAll functions are available on the top-level export.\n\n```js\nimport { compose, mapProps, withState /* ... */ } from 'recompose'\n```\n\n**Note:** `react` is a _peer dependency_ of Recompose.  If you're using `preact`, add this to your `webpack.config.js`:\n\n```js\nresolve: {\n  alias: {\n    react: \"preact\"\n  }\n}\n```\n\n### Composition\n\nRecompose helpers are designed to be composable:\n\n```js\nconst BaseComponent = props =\u003e {...}\n\n// This will work, but it's tedious\nlet EnhancedComponent = pure(BaseComponent)\nEnhancedComponent = mapProps(/*...args*/)(EnhancedComponent)\nEnhancedComponent = withState(/*...args*/)(EnhancedComponent)\n\n// Do this instead\n// Note that the order has reversed — props flow from top to bottom\nconst enhance = compose(\n  withState(/*...args*/),\n  mapProps(/*...args*/),\n  pure\n)\nconst EnhancedComponent = enhance(BaseComponent)\n```\n\nTechnically, this also means you can use them as decorators (if that's your thing):\n\n```js\n@withState(/*...args*/)\n@mapProps(/*...args*/)\n@pure\nclass Component extends React.Component {...}\n```\n\n### Optimizing bundle size\n\nSince `0.23.1` version recompose got support of ES2015 modules.\nTo reduce size all you need is to use bundler with tree shaking support\nlike [webpack 2](https://github.com/webpack/webpack) or [Rollup](https://github.com/rollup/rollup).\n\n#### Using babel-plugin-lodash\n\n[babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash) is not only limited to [lodash](https://github.com/lodash/lodash). It can be used with `recompose` as well.\n\nThis can be done by updating `lodash` config in `.babelrc`.\n\n```diff\n {\n-  \"plugins\": [\"lodash\"]\n+  \"plugins\": [\n+    [\"lodash\", { \"id\": [\"lodash\", \"recompose\"] }]\n+  ]\n }\n```\n\nAfter that, you can do imports like below without actually including the entire library content.\n\n```js\nimport { compose, mapProps, withState } from 'recompose'\n```\n\n### Debugging\n\nIt might be hard to trace how does `props` change between HOCs. A useful tip is you can create a debug HOC to print out the props it gets without modifying the base component. For example:\n\nmake\n\n```js\nconst debug = withProps(console.log)\n```\n\nthen use it between HOCs\n\n```js\nconst enhance = compose(\n  withState(/*...args*/),\n  debug, // print out the props here\n  mapProps(/*...args*/),\n  pure\n)\n```\n\n\n## Who uses Recompose\nIf your company or project uses Recompose, feel free to add it to [the official list of users](https://github.com/acdlite/recompose/wiki/Sites-Using-Recompose) by [editing](https://github.com/acdlite/recompose/wiki/Sites-Using-Recompose/_edit) the wiki page.\n\n## Recipes for Inspiration\nWe have a community-driven Recipes page. It's a place to share and see recompose patterns for inspiration. Please add to it! [Recipes](https://github.com/acdlite/recompose/wiki/Recipes).\n\n## Feedback wanted\n\nProject is still in the early stages. Please file an issue or submit a PR if you have suggestions! Or ping me (Andrew Clark) on [Twitter](https://twitter.com/acdlite).\n\n\n## Getting Help\n\n**For support or usage questions like “how do I do X with Recompose” and “my code doesn't work”, please search and ask on [StackOverflow with a Recompose tag](http://stackoverflow.com/questions/tagged/recompose?sort=votes\u0026pageSize=50) first.**\n\nWe ask you to do this because StackOverflow has a much better job at keeping popular questions visible. Unfortunately good answers get lost and outdated on GitHub.\n\nSome questions take a long time to get an answer. **If your question gets closed or you don't get a reply on StackOverflow for longer than a few days,** we encourage you to post an issue linking to your question. We will close your issue but this will give people watching the repo an opportunity to see your question and reply to it on StackOverflow if they know the answer.\n\nPlease be considerate when doing this as this is not the primary purpose of the issue tracker.\n\n### Help Us Help You\n\nOn both websites, it is a good idea to structure your code and question in a way that is easy to read to entice people to answer it. For example, we encourage you to use syntax highlighting, indentation, and split text in paragraphs.\n\nPlease keep in mind that people spend their free time trying to help you. You can make it easier for them if you provide versions of the relevant libraries and a runnable small project reproducing your issue. You can put your code on [JSBin](http://jsbin.com) or, for bigger projects, on GitHub. Make sure all the necessary dependencies are declared in `package.json` so anyone can run `npm install \u0026\u0026 npm start` and reproduce your issue.\n","funding_links":[],"categories":["JavaScript","Uncategorized","Code Design","*.js","Marks"],"sub_categories":["Uncategorized","Data Store","React","[React - A JavaScript library for building user interfaces](http://facebook.github.io/react)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facdlite%2Frecompose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facdlite%2Frecompose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facdlite%2Frecompose/lists"}