# @hocs/with-lifecycle

> Component lifecycle HOC for React and React Native

Latest version **0.5.1** (published 2019-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hocs/with-lifecycle
pnpm add @hocs/with-lifecycle
yarn add @hocs/with-lifecycle
bun add @hocs/with-lifecycle
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2019-05-23 |
| First published | 2017-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2040 |
| Author | Kir Belevich |
| Maintainers | hocs |
| Keywords | react, react-native, hoc, recompose, lifecycle |

## Links

- npm: https://www.npmjs.com/package/@hocs/with-lifecycle
- Repository: https://github.com/deepsweet/hocs
- Homepage: https://github.com/deepsweet/hocs#readme
- Issues: https://github.com/deepsweet/hocs/issues
- npm.io page: https://npm.io/package/@hocs/with-lifecycle

## Dependencies (1)

- [react-display-name](https://npm.io/package/react-display-name.md) ^0.2.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.5.1 (latest) — 2019-05-23
- 0.5.0 — 2018-06-19
- 0.4.0 — 2018-04-24
- 0.3.0 — 2017-10-09
- 0.2.0 — 2017-08-19
- 0.1.1 — 2017-08-05
- 0.1.0 — 2017-07-31

## README

# :recycle: with-lifecycle

[![npm](https://img.shields.io/npm/v/@hocs/with-lifecycle.svg?style=flat-square)](https://www.npmjs.com/package/@hocs/with-lifecycle) [![ci](https://img.shields.io/travis/deepsweet/hocs/master.svg?style=flat-square)](https://travis-ci.org/deepsweet/hocs) [![coverage](https://img.shields.io/codecov/c/github/deepsweet/hocs/master.svg?style=flat-square)](https://codecov.io/github/deepsweet/hocs) [![deps](https://david-dm.org/deepsweet/hocs.svg?path=packages/with-lifecycle&style=flat-square)](https://david-dm.org/deepsweet/hocs?path=packages/with-lifecycle)

Part of a [collection](https://github.com/deepsweet/hocs) of Higher-Order Components for React, especially useful with [Recompose](https://github.com/acdlite/recompose).

Inspired by [Reassemble](https://github.com/wikiwi/reassemble), in comparison with Recompose [`lifecycle`](https://github.com/acdlite/recompose/blob/master/docs/API.md#lifecycle) this HOC provides a handy (and limited) way to use _some_ of [React Component Lifecycle](https://reactjs.org/docs/react-component.html) methods such as:

* `onConstructor(props)`
* `onWillMount(props)`
* `onDidMount(props)`
* `onReceiveProps(props, nextProps)` – [`getDerivedStateFromProps`](https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops) as a callback but without returned state
* `onGetSnapshotBeforeUpdate(prevProps, props)` – any returned value will be passed as `snapshot` parameter to `onDidUpdate`
* `onDidUpdate(prevProps, props, snapshot)`
* `onWillUnmount(props)`
* `onDidCatch(error, info)`

So no `this`, you have no direct access to class instance anymore (:tada:).

## Install

```
yarn add @hocs/with-lifecycle
```

## Usage

```js
withLifecycle(
  methods: Object
): HigherOrderComponent
```

```js
import React from 'react';
import { compose, withState } from 'recompose';
import withLifecycle from '@hocs/with-lifecycle';

const Demo = ({ isLoading }) => (
  <h1>{ isLoading ? 'Loading' : 'Done' }</h1>
);

export default compose(
  withState('isLoading', 'setLoading', true),
  withLifecycle({
    onDidMount({ setLoading }) {
      setLoading(true, () => {
        setTimeout(() => setLoading(false), 3000);
      })
    },
    onReceiveProps(props, nextProps) {
      console.log(`isLoading: ${props.isLoading} → ${nextProps.isLoading}`);
    }
  })
)(Demo);
```

In addition, it can handle a factory function which works like Recompose [`withHandlers`](https://github.com/acdlite/recompose/blob/master/docs/API.md#withhandlers) factory:

```js
withLifecycle(
  methodsFactory: (initialProps: Object) => Object
): HigherOrderComponent
```

```js
withLifecycle(
  ({ shouldLoadOnMount }) => {
    if (shouldLoadOnMount) {
      return {
        onDidMount({ setLoading }) {
          setLoading(true, () => {
            setTimeout(() => setLoading(false), 1000);
          })
        }
      };
    }
  }
)
```

As a bonus you can "share" stuff across different lifecycle methods in that factory scope with `let mySharedStuff`, just like you did before with `this.mySharedStuff` using a class instance.

:tv: [Check out live demo](https://codesandbox.io/s/1z189o79vj).

---
_Source: https://npm.io/package/@hocs/with-lifecycle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
