Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from Kub4jz/unsubscribe
Browse files Browse the repository at this point in the history
Add unsubscribe callback as return type of mapStateToProps
  • Loading branch information
mlynch authored Nov 1, 2018
2 parents 95b9c78 + b7dcfbe commit f2b76cf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ export class MyComponent {
@State() name: string;

changeName: Action;


private unsubscribe: () => void;

componentWillLoad() {
this.store.mapStateToProps(this, (state) => {
this.unsubscribe = this.store.mapStateToProps(this, (state) => {
const {
myReducer: { name }
} = state;
Expand All @@ -88,5 +90,9 @@ export class MyComponent {
doNameChange(newName: string) {
this.changeName(newName);
}

componentDidUnload() {
this.unsubscribe();
}
}
```
2 changes: 1 addition & 1 deletion src/global/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Store {
getState: () => any;
getStore: () => any;
setStore: (any: any) => void;
mapStateToProps: (component: any, props: any) => void;
mapStateToProps: (component: any, props: any) => () => void;
mapDispatchToProps: (component: any, props: any) => void;
};

Expand Down
4 changes: 3 additions & 1 deletion src/global/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ Context.store = (function() {
});
};

_store.subscribe(() => _mapStateToProps(component, mapState));
const unsubscribe = _store.subscribe(() => _mapStateToProps(component, mapState));

_mapStateToProps(component, mapState);

return unsubscribe;
}

return {
Expand Down

0 comments on commit f2b76cf

Please sign in to comment.