By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Destroying custom object within useEffect hook
JavaScript

Javascript – Destroying custom object within useEffect hook

Admin
Last updated: 2023/12/20 at 4:33 PM
Admin
Share
2 Min Read
Destroying custom object within useEffect hook

Problem:

There is a basic vanilla js component that should be executed when element is added to DOM (componentDidMount) and destroyed when removed. Here is a basic example of such component:

Contents
Problem:Solution:
class TestComponent {
  interval?: number;
    constructor() {
    this.interval = window.setInterval(() => {
        console.log('interval is ticking')
    }, 1000);
  }
  
  destroy() {
    window.clearInterval(this.interval);
  }
}

I add it via React like so:

function TestApp() {
  const [testComponent, setTestComponent] = React.useState<TestComponent|null>(null);
  
  React.useEffect(() => {
    // initialize component when it's added to dom
    const localTestComponent = new TestComponent();
    setTestComponent(localTestComponent);
    
    return () => {
      // destroy component when it's removed
      if (testComponent) {
        testComponent.destroy();
      }
    };
  }, []);
  
  return (
    <div>Test app</div>
  );
}

For some reason it initializes twice, but never destroys (I can tell because “interval is ticking” message appears twice each second, but it should tick only once per second). Any idea why this is happening?

The destroy() method is never reached because if (testComponent) { is null in the cleanup phase.

I tried placing this component at the very root of my app and removing all third-party code, but nothing seems to help.

Solution:

Not everything should be state.

When you call setTestComponent(localTestComponent), it requires a rerender in order to change the value of testComponent. In your effect testComponent is still null. You already have the value in localTestComponent, just use it instead of testComponent. You don’t need state for that

React.useEffect(() => {
    const localTestComponent = new TestComponent();
    
    return localTestComponent.destroy
}, []);

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Is there a way to capture the row checkbox change event on AG Grid React? Javascript – Is there a way to capture the row checkbox change event on AG Grid React?
Next Article Making a shape a const, and using it multiple times in js Javascript – Making a shape a const, and using it multiple times in js
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?