reassemble is a library for the composition of React Higher-Order-Components optimized for performance.
reassemble is very similar to recompose. Conceptually both projects differ in such way that recompose uses HOCs as their building blocks, whereas reassemble uses Composables which are just a collection of callbacks. Most noticeably using reassemble only results in a single Higher-Order-Component and thus has a significant higher performance. It also solves the problem of Dev Tools Ballooning which is an issue in recompose.
Both projects are not mutual exclusive but reassemble can be used perfectly together with recompose. In the end reassemble just produces a Higher-Order-Component that fits in nicely with the composition tools of recompose.
At the moment recompose is a bit faster in simple compositions (though we plan to close this gap) and reassemble performs better in complex composition.
npm install reassemble --saveimport { assemble, withState, mapProps } from "reassemble"
const enhance = assemble(
withState(/*...args*/),
mapProps(/*...args*/),
);
const EnhancedComponent = enhance(BaseComponent);Note: assemble is also exported with the alias compose to allow easy transition from recompose to reassemble
reassemble exports also as ES6 modules and as such tree shaking (e.g. with webpack 2) can be used to effectively reduce file size.
Without tree shaking you can import the modules explicitly:
import mapProps from "reassemble/lib/mapProps"
import withState from "reassemble/lib/withState"And for ES5 projects:
const mapProps = require("reassemble/cjs/mapProps").mapProps
const withState = require("reassemble/cjs/withState").withStateMultiple Composables can be combined into one using combine() which makes it easy to define your own:
export const withClickCounter = combine(
withState('counter', 'setCounter', 0),
withHandlers({
onClick: ({counter, setCounter}) => setCounter(counter + 1),
}),
);This is also useful for some Composables like branch that takes another Composable as an argument.
Most of the Composables supports the use of ES6 Symbols. You can use Symbols to pass hidden props among your Composables.
* In some cases TypeScript users will lose type information.
reassemble is written in TypeScript and as such comes with its own definitions. They do not follow the same type definitions as recompose so some manual work is required here.
| Name | Support | Remarks |
|---|---|---|
| branch | ✅ | |
| defaultProps | ✅ | |
| flattenProps | ✅ | |
| getContext | ✅ | |
| lifecycle | ❌ | Use Lifecycle Composables |
| mapProps | ✅ | |
| mapPropsStream | ❌ | File an issue if you really need this |
| onlyUpdateForKeys | ✅ | |
| onlyUpdateForPropTypes | ❌ | Use onlyUpdateForKeys instead |
| renameProp | ✅ | |
| renameProps | ✅ | |
| renderComponent | ✅ | |
| renderNothing | ✅ | |
| setDisplayName | ✅ | |
| setPropTypes | ✅ | |
| setStatic | ✅ | |
| shouldUpdate | ✅ | |
| pure | ✅ | |
| withContext | ✅ | Context will not be available in other Composables of the same Component |
| withHandlers | ✅ | |
| withProps | ✅ | |
| withPropsOnChange | ✅ | |
| withReducer | ✅ | |
| withState | ✅ | |
| toClass | ✅ |
debug()noOpomitProps()isolate()integrate()onWillMount()onDidMount()onWillUnmount()onWillReceiveProps()onWillUpdate()onDidUpdate()
debug(callback: (props) => void): ComposableRuns callback with current props. Defaults to logging to the console.
noOp: ComposableomitProps(...keys: string[]): ComposableOmit selected props.
isolate(...composables: Composable[]): ComposableRuns passed Composables in isolation: any props created will be reverted.
Use with integrate() to selectively keep props.
isolate(
withProps({
a: 1,
b: 2,
}),
integrate("b"),
)
// { b: 3 }integrate(...keys: string[]): ComposableSelectively keep props that are otherwise reverted in isolate().
Warning: Lifecycle Composables are still experimental.
onWillMount(props): ComposableCalled during lifecycle componentWillMount()
onDidMount(props): ComposableCalled during lifecycle componentDidMount()
onWillUnmount(props): ComposableCalled during lifecycle componentWillUnmount()
onWillReceiveProps(prevProps, nextProps): ComposableCalled during lifecycle componentWillReceiveProps() and when state changes because some props are derived from state.
onWillUpdate(prevProps, nextProps): ComposableCalled during lifecycle componentWillUpdate()
onDidUpdate(prevProps, nextProps): ComposableCalled during lifecycle componentDidUpdate()
- Compatibility with React Fiber
- Improve Lifecycle handling
- More performance optimizations
- More tests
MIT