Skip to content

Memory leaks when component unmounts #1

Closed
@sudkumar

Description

@sudkumar

Following line will cause memory leaks when component unmounts before completing the counter lifecycle.

This should be handled in the following way.

  componentWillUnmount () {
    this.rafHandle && raf.cancel(this.rafHandle)
  }
  incrementCounter (counter?: number = 0) {
    const { to, duration, strict } = this.props
    if (this.state.count < to) {
      this.setState(({ count: number }): ({ count: number }) => ({
        count: this.easeFn(counter, 0, to, duration)
      }), () => {
        const timeBeforeRag: number = getCurrentTime()
        this.rafHandle = raf(() : void => {
          const timeAtInc: number = getCurrentTime()
          this.incrementCounter(counter + (strict ? timeAtInc - timeBeforeRag : 16))
        })
      })
    }
  }

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions