UNPKG

4.01 kBMarkdownView Raw
1# `@shopify/react-compose`
2
3[![Build Status](https://github.com/Shopify/quilt/workflows/Node-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ANode-CI)
4[![Build Status](https://github.com/Shopify/quilt/workflows/Ruby-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ARuby-CI)
5[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Freact-compose.svg)](https://badge.fury.io/js/%40shopify%2Freact-compose) ![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/@shopify/react-compose.svg)
6
7Cleanly compose multiple component enhancers together with minimal fuss.
8
9## Installation
10
11```bash
12$ yarn add @shopify/react-compose
13```
14
15## Usage
16
17This module exports a single default function `compose`.
18
19```ts
20import compose from '@shopify/react-compose';
21```
22
23This function can be called on a list of component enhancers ([Higher Order Components](https://reactjs.org/docs/higher-order-components.html)) to return a single master component enhancer that adds all of the props from all of the enhancers you gave it.
24
25```ts
26import {withRouter} form 'react-router';
27import compose from '@shopify/react-compose';
28import {withMousePosition} from './mouse-position';
29
30const enhancer = compose(
31 withRouter,
32 withMousePosition,
33);
34
35class SomeComponent extends React.Component {
36 ...
37}
38
39// this will be the same as withRouter(withMousePosition(SomeComponent))
40export default compose(withRouter, withMousePosition)(SomeComponent);
41```
42
43This enhancer will act roughly the same as calling each enhancer in turn. This can save a lot of boilerplate for cases where each enhancer comes from it's own factory with config.
44
45```ts
46// In this example each enhancer is actually a factory that takes config.
47const EnhancedComponent = enhancerOne(someConfig)(
48 enhancerTwo(otherConfig)(enhancerThree(moreConfig)(Component)),
49);
50
51// We can clean this up greatly using compose
52const EnhancedComponent = compose(
53 enhancerOne(someConfig),
54 enhancerTwo(otherconfig),
55 enhancerThree(moreConfig),
56)(Component);
57```
58
59## Differences from other `compose` implementations
60
61[Apollo](https://www.apollographql.com/docs/react/api/react-apollo.html#compose), [Redux](https://redux.js.org/api-reference/compose), and [Recompose](https://github.com/acdlite/recompose/blob/master/docs/API.md) also export their own `compose` function. This can be perfectly fine for many usecases, however, this implementation has some advantages (in our opinions).
62
63### Standalone
64
65If you are not using Apollo, Redux, or Recompose, you could still have other enhancers you want to combine. This library is only a few lines long and only depends on `hoist-non-react-statics` (with a peer-dependency on `React`), so you can relatively weightlessly add it to your project even if you are dependency light.
66
67### Less cumbersome Typescript implementation
68
69The Typescript definition for other `compose` functions takes a number of generic parameters equal to the number of enhancers you pass in. This means you can easily end up with something like
70
71```ts
72compose<Props & FooProps & BarProps, Props & FooProps, Props>(
73 FooEnhancer,
74 BarEnhancer,
75)(Component);
76```
77
78which is difficult to maintain and understand. It's usually fine from a consumers perspective to just define the _output_ props for these types of statements, and the definition for `compose` from this package can be used in this scenario with significantly less type annotations.
79
80```ts
81compose<Props>(FooEnhancer, BarEnhancer)(Component);
82```
83
84### Static hoisting
85
86Apollo's `compose` function does not hoist static members. If you want to do something like make subcomponents available as static members you would need to attach them manually to the enhanced version of the component.
87
88With this implementation you can be sure any static properties on your classical components will be hoisted up to the wrapper Component.