Skip to content

Arrow-style ngOnDestroy not called #38087

Closed

Description

@jzabinski

🐞 bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

No

Description

I'm looking for feedback on this issue as much as reporting a potential bug.
If ngOnDestroy is called like this:
ngOnDestroy() { [some code] }
Then it works as expected. If ngOnDestroy is called like this:
ngOnDestroy = () => [some code]
Then it is not called.

Note that the Typescript linter is actually okay with either style, even though the interface uses:
ngOnDestroy(): void

If you're like me, you would assume that an arrow style function can be used for a lifecycle hook without actually trying it. The issue would be undetectable until you notice that the hook isn't working.

🔬 Minimal Reproduction

https://stackblitz.com/edit/whynoarrowstyle
Two components meet in the woods, one using an arrow style function, and one not using that function type. Click 'Destroy the below'. Notice that the code written in ngOnDestroy is called for the funcStyleComponent, but not the arrowStyleComponent.

🔥 Exception or Error

No error, more just looking for an explanation of the existing behavior, and whether more can be done to make it clear that using arrow style functions as lifecycle hooks will be invisibly undetectable.

🌍 Your Environment

Angular Version:



     _                      _                 ____ _     ___ 
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 9.1.7
Node: 12.13.1
OS: win32 x64

Angular: 9.1.9
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.7
@angular-devkit/build-angular     0.901.7
@angular-devkit/build-optimizer   0.901.7
@angular-devkit/build-webpack     0.901.7
@angular-devkit/core              9.1.7
@angular-devkit/schematics        9.1.7
@angular/cli                      9.1.7
@angular/fire                     6.0.0
@ngtools/webpack                  9.1.7
@schematics/angular               9.1.7
@schematics/update                0.901.7
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0

Anything else relevant?
ngOnDestroy is often used to unsubscribe, and its code can often be reduced to one line. If lifecycle hooks were expressible as arrow style functions, then we could express it in one line, like so:
ngOnDestroy = () => this.subscriptions.unsubscribe();
(Granted, you can say the same thing using classic functions as ngOnDestroy() { this.subscriptions.unsubscribe; }, but formatters love to break that into multiple lines.)

Activity

  1. added this to the needsTriage milestone on Jul 15, 2020
  2. Airblader commented on Jul 16, 2020

    @Airblader
    Contributor

    Possible duplicate of #38043

  3. spock123 commented on Jul 17, 2020

    @spock123

    @jzabinski afair there are issues with fat arrow functions and class inheritance (I might be mistaken).

  4. pkozlowski-opensource commented on Jul 27, 2020

    @pkozlowski-opensource
    Member

    This is indeed a duplicate of #38043, thnx @Airblader! Since this is quite a common request I've opened #38241 with more background info

  5. angular-automatic-lock-bot commented on Aug 27, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions