npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-timeout

v2.0.1

Published

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

Downloads

87,436

Readme

React Timeout

Greenkeeper badge travis build npm version

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

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.

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

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

With Annotations

@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

The timer mixin recommended by the 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.

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