# react-timeout

> HOC for React and React Native providing versions of setTimeout etc. that cancels when unmounted.

Latest version **2.0.1** (published 2021-01-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-timeout
pnpm add react-timeout
yarn add react-timeout
bun add react-timeout
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2021-01-11 |
| First published | 2015-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-timeout) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 126 |
| Author | plougsgaard |
| Maintainers | plougsgaard |
| Keywords | react, reactjs, native, timer, timeout, hoc |

## Links

- npm: https://www.npmjs.com/package/react-timeout
- Repository: https://github.com/plougsgaard/react-timeout
- Issues: https://github.com/plougsgaard/react-timeout/issues
- npm.io page: https://npm.io/package/react-timeout

## Dependencies (2)

- [object-assign](https://npm.io/package/object-assign.md) ^4.0.1
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^2.3.1

## 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

- 2.0.1 (latest) — 2021-01-11
- 2.0.0 — 2020-10-08
- 1.2.0 — 2019-02-13
- 1.1.2 — 2018-09-06
- 1.1.1 — 2018-03-01
- 1.1.0 — 2018-02-28
- 1.0.1 — 2017-05-06
- 1.0.0 — 2016-04-16
- 0.16.1 — 2016-04-15
- 0.16.0 — 2015-12-06
- 0.14.3 — 2015-11-19
- 0.14.0 — 2015-11-18
- 0.13.3 — 2015-09-08

## README

# React Timeout

[![Greenkeeper badge](https://badges.greenkeeper.io/plougsgaard/react-timeout.svg)](https://greenkeeper.io/) [![travis build](https://img.shields.io/travis/plougsgaard/react-timeout.svg)](https://travis-ci.org/plougsgaard/react-timeout) [![npm version](https://badge.fury.io/js/react-timeout.svg)](https://badge.fury.io/js/react-timeout)

> Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

> Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Seeing a lot of the above? If so this might be useful!

React Timeout is a higher order component for [React](https://github.com/facebook/react) and [React Native](https://github.com/facebook/react-native) providing the wrapped component with **safe** versions of

Set                     | Clear
------------------------|------------------------
`setTimeout`            | `clearTimeout`
`setInterval`           | `clearInterval`
`setImmediate`          | `clearImmediate`
`requestAnimationFrame` | `cancelAnimationFrame`

When the wrapped component is *unmounted*, any lingering timers will be canceled automatically.

# Installation

```
npm install --save react-timeout
```

## Version Compatibility

| React     |  React Timeout |
| :-------  | :------------: |
| `16.3`+   | `2+`           |
| <`16.3`   | `1.x`          |

# Usage

## React / React Native

```javascript
import ReactTimeout from 'react-timeout'
```

# Examples

## ES6 Classes - "The Light Switch"

The component simulates a light switch. It has a state `on` which is `true` or `false`. When the button is clicked it waits `5000ms` before switching the `on` state.

```javascript
import React from 'react'
import ReactTimeout from 'react-timeout'

class LightSwitchExample extends React.Component {
  state = {
    on: false
  }
  toggle = () => {
    this.setState({ on: !this.state.on })
  }
  handleClick = (e) => {
    this.props.setTimeout(this.toggle, 5000) // call the `toggle` function after 5000ms
  }
  render () {
    return (
      <div style={{ backgroundColor: (this.state.on ? 'yellow' : 'gray') }}>
        <button onClick={this.handleClick}>Click me!</button>
      </div>
    )
  }
}
export default ReactTimeout(LightSwitchExample)
```

If the component is unmounted before the `5000ms` is up, the timeout is canceled by `ReactTimeout`.

Had we just called the regular old `setTimeout`, the callback `toggle` would still fire and try setting the state of an unmounted component.

## Functional Stateless Components

```javascript
const Example = (props) => {
  return (
    <button
      onClick={() => props.setTimeout(..)}>Click me!</button>
  )
}
export default ReactTimeout(Example)
```

## With Annotations

```javascript
@ReactTimeout
class Example extends React.Component {
  render () {
    return (
      <button
        onClick={() => this.props.setTimeout(..)}>Click me!</button>
    )
  }
}
```

## Accessing the wrapped instance

You can access the wrapped instance using `React.createRef` from version `2+`.

If you're using version `1.x` you can access the wrapped instance with `component.getWrappedInstance()`.

# Something similar

## [react-timer-mixin](https://github.com/reactjs/react-timer-mixin)

The timer mixin recommended by the  [react-native](https://github.com/facebook/react-native) docs.

## React Native 0.17 and below

Only supported by version `1.x`.

If you're using a version of React Native that is `0.17` or below you have to import from the `/native` namespace.

```javascript
import ReactTimeout from 'react-timeout/native' // only for react-native 0.17 or below
```

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