How to handle unmounting a component with set interval to avoid a memory leak

Barry Michael Doyle

I have a React component that starts with a count down timer where the component gets unmounted either when a button is pressed or the countdown reaches zero. It's implemente like this:

state = {
  counter: 60,
};

componentWillMount() {
  this.doCountDown();
}

doCountDown() {
  if (this.state.counter < 1) {
    return this.props.tryAgain();
  }
  this.setState(prevState => ({ counter: prevState.counter - 1 }));
  setTimeout(() => this.doCountDown(), 1000);
}

This isn't a duplicate of Unmounting a Component with a SetInterval in React.

In my case here I have a recursive event going on so I can't clear the interval like the example in the link mentioned.

Anyone have any ideas on what to do?

For more explanation: this.props.tryAgain() will change the state higher up which causes this component to unmount. When this occurs, due to the timeout, the state still attempts to change while the component has already unmounted. This indicates a memory leak and is bad practice. So I'm trying to find out a good work around to prevent that from happening.

Clément Prévost

You need to call the call the clearInterval method on unmount:

componentWillUnmount() {
    clearInterval(this.timeout)
}


doCountDown() {
    if (this.state.counter < 1) {
        return this.props.tryAgain();
    }
    this.setState(prevState => ({ counter: prevState.counter - 1 }));
    this.timeout = setTimeout(() => this.doCountDown(), 1000);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid this memory leak?

From Dev

How to release memory in android to avoid memory leak

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

how to avoid memory leak in dynamically allocated widget

From Dev

How can I fix to avoid a memory leak?

From Dev

How can I avoid a memory leak in this function?

From Dev

How to avoid a memory leak with __block and completion blocks

From Dev

javascript interval memory leak

From Dev

Avoid Memory leak

From Dev

How to avoid memory leak with shared_ptr and SWIG

From Dev

How to avoid memory leak at design-implementation level

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

How to avoid a memory leak by using pthread_cancel?

From Dev

Node and RxJs: How can I avoid a memory leak on a long process?

From Dev

Avoid memory leak with WeakReference Android

From Dev

Why does this Swift code leak memory, and how do I handle it?

From Dev

Handle COM object memory leak

From Dev

How to block this memory leak?

From Dev

How to detect a memory leak?

From Dev

nodejs setMaxListeners to avoid memory leak detection

From Dev

Ways to avoid memory leak when exception thrown

From Dev

weak to non class types to avoid memory leak

From Dev

Is setting ulimit -v sufficient to avoid memory leak

From Dev

How does systemd handle unmounting /usr on shutdown, since it is mounted in the initramfs?

From Dev

How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?

From Dev

WPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak

From Dev

How to avoid memory leak when passing function-returned pointer as input to other function?

From Dev

Delay in component unmounting

From Dev

How can I prevent React from unmounting/remounting a component?

Related Related

  1. 1

    How to avoid this memory leak?

  2. 2

    How to release memory in android to avoid memory leak

  3. 3

    How to avoid a memory leak with __block and completion blocks

  4. 4

    how to avoid memory leak in dynamically allocated widget

  5. 5

    How can I fix to avoid a memory leak?

  6. 6

    How can I avoid a memory leak in this function?

  7. 7

    How to avoid a memory leak with __block and completion blocks

  8. 8

    javascript interval memory leak

  9. 9

    Avoid Memory leak

  10. 10

    How to avoid memory leak with shared_ptr and SWIG

  11. 11

    How to avoid memory leak at design-implementation level

  12. 12

    Node and RxJs: How can I avoid a memory leak on a long process?

  13. 13

    How to avoid a memory leak by using pthread_cancel?

  14. 14

    Node and RxJs: How can I avoid a memory leak on a long process?

  15. 15

    Avoid memory leak with WeakReference Android

  16. 16

    Why does this Swift code leak memory, and how do I handle it?

  17. 17

    Handle COM object memory leak

  18. 18

    How to block this memory leak?

  19. 19

    How to detect a memory leak?

  20. 20

    nodejs setMaxListeners to avoid memory leak detection

  21. 21

    Ways to avoid memory leak when exception thrown

  22. 22

    weak to non class types to avoid memory leak

  23. 23

    Is setting ulimit -v sufficient to avoid memory leak

  24. 24

    How does systemd handle unmounting /usr on shutdown, since it is mounted in the initramfs?

  25. 25

    How to avoid memory leak in context.getSystemService(Context.CAMERA_SERVICE)?

  26. 26

    WPF 4.5: how to remove weak reference caused by binding to an object in order to avoid memory leak

  27. 27

    How to avoid memory leak when passing function-returned pointer as input to other function?

  28. 28

    Delay in component unmounting

  29. 29

    How can I prevent React from unmounting/remounting a component?

HotTag

Archive