Gathering detailed insights and metrics for recompose
Gathering detailed insights and metrics for recompose
Gathering detailed insights and metrics for recompose
Gathering detailed insights and metrics for recompose
@types/recompose
TypeScript definitions for recompose
@nivo/recompose
[![version](https://img.shields.io/npm/v/@nivo/recompose.svg?style=flat-square)](https://www.npmjs.com/package/@nivo/recompose)
@types/react-recompose
TypeScript definitions for react-recompose
@types/shakacode__recompose
TypeScript definitions for @shakacode/recompose
A React utility belt for function components and higher-order components.
npm install recompose
59.6
Supply Chain
68.8
Quality
77.1
Maintenance
50
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
14,750 Stars
648 Commits
1,253 Forks
172 Watching
50 Branches
102 Contributors
Updated on 27 Nov 2024
JavaScript (98.9%)
HTML (1.1%)
Cumulative downloads
Total Downloads
Last day
-0.8%
170,464
Compared to previous day
Last week
7.8%
887,054
Compared to previous week
Last month
14.5%
3,560,726
Compared to previous month
Last year
-13.3%
42,519,438
Compared to previous year
6
1
Hi! I created Recompose about three years ago. About a year after that, I joined the React team. Today, we announced a proposal for 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 and @istarkov for their heroic work maintaining Recompose over the last few years.
Read more discussion about this decision here.
Recompose is a React utility belt for function components and higher-order components. Think of it like lodash for React.
Full API documentation - Learn about each helper
Recompose Base Fiddle - Easy way to dive in
npm install recompose --save
📺 Watch Andrew's talk on Recompose at React Europe. (Note: Performance optimizations he speaks about have been removed, more info here)
recompose-relay — Recompose helpers for Relay
Helpers like withState()
and withReducer()
provide a nicer way to express state updates:
1const enhance = withState('counter', 'setCounter', 0) 2const Counter = enhance(({ counter, setCounter }) => 3 <div> 4 Count: {counter} 5 <button onClick={() => setCounter(n => n + 1)}>Increment</button> 6 <button onClick={() => setCounter(n => n - 1)}>Decrement</button> 7 </div> 8)
Or with a Redux-style reducer:
1const counterReducer = (count, action) => { 2 switch (action.type) { 3 case INCREMENT: 4 return count + 1 5 case DECREMENT: 6 return count - 1 7 default: 8 return count 9 } 10} 11 12const enhance = withReducer('counter', 'dispatch', counterReducer, 0) 13const Counter = enhance(({ counter, dispatch }) => 14 <div> 15 Count: {counter} 16 <button onClick={() => dispatch({ type: INCREMENT })}>Increment</button> 17 <button onClick={() => dispatch({ type: DECREMENT })}>Decrement</button> 18 </div> 19)
Helpers like componentFromProp()
and withContext()
encapsulate common React patterns into a simple functional interface:
1const enhance = defaultProps({ component: 'button' }) 2const Button = enhance(componentFromProp('component')) 3 4<Button /> // renders <button> 5<Button component={Link} /> // renders <Link />
1const provide = store => withContext( 2 { store: PropTypes.object }, 3 () => ({ store }) 4) 5 6// Apply to base component 7// Descendants of App have access to context.store 8const AppWithContext = provide(store)(App)
No need to write a new class just to implement shouldComponentUpdate()
. Recompose helpers like pure()
and onlyUpdateForKeys()
do this for you:
1// A component that is expensive to render 2const ExpensiveComponent = ({ propA, propB }) => {...} 3 4// Optimized version of same component, using shallow comparison of props 5// Same effect as extending React.PureComponent 6const OptimizedComponent = pure(ExpensiveComponent) 7 8// Even more optimized: only updates if specific prop keys have changed 9const HyperOptimizedComponent = onlyUpdateForKeys(['propA', 'propB'])(ExpensiveComponent)
Recompose helpers integrate really nicely with external libraries like Relay, Redux, and RxJS
1const enhance = compose( 2 // This is a Recompose-friendly version of Relay.createContainer(), provided by recompose-relay 3 createContainer({ 4 fragments: { 5 post: () => Relay.QL` 6 fragment on Post { 7 title, 8 content 9 } 10 ` 11 } 12 }), 13 flattenProp('post') 14) 15 16const Post = enhance(({ title, content }) => 17 <article> 18 <h1>{title}</h1> 19 <div>{content}</div> 20 </article> 21)
Many React libraries end up implementing the same utilities over and over again, like shallowEqual()
and getDisplayName()
. Recompose provides these utilities for you.
1// Any Recompose module can be imported individually 2import getDisplayName from 'recompose/getDisplayName' 3ConnectedComponent.displayName = `connect(${getDisplayName(BaseComponent)})` 4 5// Or, even better: 6import wrapDisplayName from 'recompose/wrapDisplayName' 7ConnectedComponent.displayName = wrapDisplayName(BaseComponent, 'connect') 8 9import toClass from 'recompose/toClass' 10// Converts a function component to a class component, e.g. so it can be given 11// a ref. Returns class components as is. 12const ClassComponent = toClass(FunctionComponent)
Forget ES6 classes vs. createClass()
.
An idiomatic React application consists mostly of function components.
1const Greeting = props => 2 <p> 3 Hello, {props.name}! 4 </p>
Function components have several key advantages:
setState()
API, favoring props instead.(Note that although Recompose encourages the use of function components whenever possible, it works with normal React components as well.)
Most of the time when we talk about composition in React, we're talking about composition of components. For example, a <Blog>
component may be composed of many <Post>
components, which are composed of many <Comment>
components.
Recompose 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.
Recompose provides a toolkit of helper functions for creating higher-order components.
All functions are available on the top-level export.
1import { compose, mapProps, withState /* ... */ } from 'recompose'
Note: react
is a peer dependency of Recompose. If you're using preact
, add this to your webpack.config.js
:
1resolve: { 2 alias: { 3 react: "preact" 4 } 5}
Recompose helpers are designed to be composable:
1const BaseComponent = props => {...}
2
3// This will work, but it's tedious
4let EnhancedComponent = pure(BaseComponent)
5EnhancedComponent = mapProps(/*...args*/)(EnhancedComponent)
6EnhancedComponent = withState(/*...args*/)(EnhancedComponent)
7
8// Do this instead
9// Note that the order has reversed — props flow from top to bottom
10const enhance = compose(
11 withState(/*...args*/),
12 mapProps(/*...args*/),
13 pure
14)
15const EnhancedComponent = enhance(BaseComponent)
Technically, this also means you can use them as decorators (if that's your thing):
1@withState(/*...args*/) 2@mapProps(/*...args*/) 3@pure 4class Component extends React.Component {...}
Since 0.23.1
version recompose got support of ES2015 modules.
To reduce size all you need is to use bundler with tree shaking support
like webpack 2 or Rollup.
babel-plugin-lodash is not only limited to lodash. It can be used with recompose
as well.
This can be done by updating lodash
config in .babelrc
.
1 { 2- "plugins": ["lodash"] 3+ "plugins": [ 4+ ["lodash", { "id": ["lodash", "recompose"] }] 5+ ] 6 }
After that, you can do imports like below without actually including the entire library content.
1import { compose, mapProps, withState } from 'recompose'
It 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:
make
1const debug = withProps(console.log)
then use it between HOCs
1const enhance = compose( 2 withState(/*...args*/), 3 debug, // print out the props here 4 mapProps(/*...args*/), 5 pure 6)
If your company or project uses Recompose, feel free to add it to the official list of users by editing the wiki page.
We have a community-driven Recipes page. It's a place to share and see recompose patterns for inspiration. Please add to it! Recipes.
Project 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.
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 first.
We 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.
Some 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.
Please be considerate when doing this as this is not the primary purpose of the issue tracker.
On 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.
Please 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 or, for bigger projects, on GitHub. Make sure all the necessary dependencies are declared in package.json
so anyone can run npm install && npm start
and reproduce your issue.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 16/24 approved changesets -- score normalized to 6
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
172 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More