Coder Social home page Coder Social logo

react-timer-mixin's Introduction

react-timer-mixin

Using bare setTimeout, setInterval, setImmediate and requestAnimationFrame calls is very dangerous because if you forget to cancel the request before the component is unmounted, you risk the callback throwing an exception.

If you include TimerMixin, then you can replace your calls to setTimeout(fn, 500) with this.setTimeout(fn, 500) (just prepend this.) and everything will be properly cleaned up for you.

Installation

Install the module directly from npm:

npm install react-timer-mixin

Example

var React = require('react');
var TimerMixin = require('react-timer-mixin');

var Component = React.createClass({
  mixins: [TimerMixin],
  componentDidMount() {
    this.setTimeout(
      () => { console.log('I do not leak!'); },
      500
    );
  }
});

react-timer-mixin's People

Contributors

plougsgaard avatar sophiebits avatar tadeuzagallo avatar zpao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-timer-mixin's Issues

Requiring unknown module "react-timer-mixin".

Embed React Native inside Native Apps, install React Native by using CocoaPods. It works well, but when I add " var TimerMixin = require('react-timer-mixin'); ",it came out
Requiring unknown module "react-timer-mixin".

I exec�uted " npm install react-timer-mixin " ,but it didn't work.

how to done pause and reset using react-timer-mixin

hai i am using this module for pause and reset time but i am struggling that how to pause the timer,stop the timer,reset the timer, here i am attaching the image of the view

demo-timer

plaese help me how to done i used

TimerMixin.setTimeout(
      () => { console.log('I do not leak!'); },
      5000
    );

but timer not stop,help me that how to pause the timer

Why is this necessary?

Can't you just use a private variable on the component for your timers, and then use the lifecycle method, componentWillUnmount, to clear appropriate "timeouts" and "intervals"?

This mixin is uses more power than it needs to. This use case could be solved simply by using a documented & official lifecycle method.

Receiving a warning from TimerMixin after unmounting the component.

Appears not to work properly, perhaps because used as a @mixin() ?

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. Please check the code for the undefined component.
warning @ warning.js?8a56:44
getInternalInstanceReadyForUpdate @ ReactUpdateQueue.js?fd2c:45
ReactUpdateQueue.enqueueSetState @ ReactUpdateQueue.js?fd2c:192
ReactComponent.setState @ ReactComponent.js?702a:67
_handleDismiss @ AlertBanner.js?c408:195
(anonymous function) @ TimerMixin.js?fbdb:18
.

import { mixin } from 'core-decorators'
import TimerMixin from 'react-timer-mixin'

@mixin(TimerMixin)
class AlertBanner {
render () {
this._setupDismissTimer()
...
}
_setupDismissTimer () {
let theTimer = this._dismissTimer
if (this.state.autoDismiss
&& this._isShowing(this.state.transition)) {
if (!theTimer) {
theTimer = this.setTimeout(
this._handleDismiss
, TIMEOUT)
}
}
else {
this._clearDismissTimer()
}
this._dismissTimer = theTimer
}
@autoBind
_handleMouseEnter () {
this._clearDismissTimer()
}
@autoBind
_handleMouseLeave () {
this._setupDismissTimer()
}
_clearDismissTimer () {
const timer = this._dismissTimer
this._dismissTimer = void 0
if (timer) {
this.clearTimeout(timer)
}
}
}

I have to add this method to prevent an error, but I thought that was the point of this mixin to not have to clear any timers before we unmount.

componentWillUnmount() {
    this._clearDismissTimer()
}

Supporting TimerMixin for class syntax'd React components

Right now mixins are not supported in react component that follow es6 class syntax.

Should we be writing a timermixin that can be extended by a react component with class syntax or can we use the current mixin as a base class?

Remove Patent grant

Last year, Facebook actively dropped the patent grant from e.g. React. Unfortunately, this package still contains it, which raises the alarms we have set up.
Would it be possible to drop the patent grant?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.