InfomediaLtd / angular2-redux

57 stars 12 forks source link

Documention of "unsubscribe"? #3

Closed maku closed 8 years ago

maku commented 8 years ago

From my point of view, It would a good idea to document how to subscribe / unsubscribe correctly or at least mention that it is necessary.

e.g in a component:

import {AppStore} from 'angular2-redux';

/**
 * Base Component which manages redux store with implementing lifecycle methods
 */
export class StateAwareComponent implements OnDestroy {

    appStoreDisposer: Function;

    constructor(public appStore: AppStore) {
        this.appStoreDisposer = appStore.subscribe(state => {
            this.handleStateSubscription(state);
        });
    }

    handleStateSubscription(state: any): void {
        console.error('handleState method not implemented');
    }

    ngOnDestroy() {
        console.log('destroy called');
        this.appStoreDisposer();
    }
}
rubyboy commented 8 years ago

@maku very good point. Thanks for pointing this out!

I've added some code to the example app unsubscribing from the app store and also some documentation: image