Recompose
Recompose is a React utility belt for function components and higher-order components. Think of it like lodash for React.
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)
Related modules
recompose-relay — Recompose helpers for Relay
You can use Recompose to...
...lift state into functional wrappers
Helpers like withState()
and withReducer()
provide a nicer way to express state updates:
Or with a Redux-style reducer:
...perform the most common React patterns
Helpers like componentFromProp()
and withContext()
encapsulate common React patterns into a simple functional interface:
...optimize rendering performance
No need to write a new class just to implement shouldComponentUpdate()
. Recompose helpers like pure()
and onlyUpdateForKeys()
do this for you:
...interoperate with other libraries
Recompose helpers integrate really nicely with external libraries like Relay, Redux, and RxJS
...build your own libraries
Many React libraries end up implementing the same utilities over and over again, like shallowEqual()
and getDisplayName()
. Recompose provides these utilities for you.
...and more
Translation
Why
Forget ES6 classes vs. createClass()
.
An idiomatic React application consists mostly of function components.
Function components have several key advantages:
- They help prevent abuse of the
setState()
API, favoring props instead. - They encourage the "smart" vs. "dumb" component pattern.
- They encourage code that is more reusable and modular.
- They discourage giant, complicated components that do too many things.
- In the future, they will allow React to make performance optimizations by avoiding unnecessary checks and memory allocations.
(Note that although Recompose encourages the use of function components whenever possible, it works with normal React components as well.)
Higher-order components made easy
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.
Should I use this? Performance and other concerns
Usage
All functions are available on the top-level export.
Note: react
is a peer dependency of Recompose. If you're using preact
, add this to your webpack.config.js
:
Composition
Recompose helpers are designed to be composable:
Technically, this also means you can use them as decorators (if that's your thing):
Optimizing bundle size
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.
Using babel-plugin-lodash
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
.
After that, you can do imports like below without actually including the entire library content.
Debugging
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
then use it between HOCs