How to manage component lifecycle hooks for resource cleanup

0 votes
Can you tell me How to manage component lifecycle hooks for resource cleanup?
3 days ago in Node-js by Nidhi
• 14,600 points
34 views

1 answer to this question.

0 votes

You manage component lifecycle hooks—especially ngOnDestroy()—to perform resource cleanup such as:

Unsubscribing from observables

Clearing intervals/timeouts

Detaching event listeners

Releasing memory-heavy objects

Best Lifecycle Hook for Cleanup: ngOnDestroy()

Example: Unsubscribing from a Subscription

import { Component, OnDestroy } from '@angular/core';

import { Subscription } from 'rxjs';

@Component({

  selector: 'app-example',

  template: `...`

})

export class ExampleComponent implements OnDestroy {

  private dataSubscription: Subscription;

  constructor(private dataService: SomeService) {

    this.dataSubscription = this.dataService.getData()

      .subscribe(data => console.log(data));

  }

  ngOnDestroy() {

    if (this.dataSubscription) {

      this.dataSubscription.unsubscribe();

    }

    console.log('Component destroyed and resources cleaned up.');

  }

}

answered 2 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer

how to manage complex redux state for different async calls?

To manage complex Redux state for different ...READ MORE

answered Mar 21 in Node-js by Anvi
50 views
0 votes
1 answer
0 votes
1 answer

How to create .pem files for https web server?

Hello @kartik, The two files you need are ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
3,576 views
0 votes
1 answer

How to setup route for websocket server in express?

Hello @kartik, You'll want to use the path option: var wss ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
5,717 views
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 2 days ago in Node-js by anonymous
26 views
0 votes
1 answer
0 votes
1 answer

How to implement a directive that auto-saves form data periodically?

To create a directive that automatically saves ...READ MORE

answered 2 days ago in Node-js by anonymous
28 views
0 votes
1 answer

How to develop a directive that restricts user input based on custom conditions?

To create an Angular directive that restricts ...READ MORE

answered 2 days ago in Node-js by anonymous
29 views
0 votes
1 answer

How to manage state within a React component?

In React, state is managed differently in ...READ MORE

answered Mar 26 in Node-js by anonymous
73 views
0 votes
1 answer

How to implement component lifecycle methods in a Class Component?

To implement component lifecycle methods in a ...READ MORE

answered Mar 26 in Node-js by anonymous
74 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP