Skip to content

Instantly share code, notes, and snippets.

@kakajika
Last active November 22, 2016 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakajika/8e0375ef5e2c728edb16387ac9244f65 to your computer and use it in GitHub Desktop.
Save kakajika/8e0375ef5e2c728edb16387ac9244f65 to your computer and use it in GitHub Desktop.
export function RxAutoDispose<T extends React.ComponentClass<any>>(target: T): T {
target.prototype.disposer = new Rx.Subscription()
const originalMount = target.prototype.componentDidMount
Object.defineProperty(target.prototype, "componentDidMount", {
value: function() {
console.log("componentDidMount")
return originalMount.apply(this, arguments)
}
})
const originalUnmount = target.prototype.componentWillUnmount
Object.defineProperty(target.prototype, "componentWillUnmount", {
value: function() {
console.log("componentWillUnmount")
target.prototype.disposer.unsubscribe()
target.prototype.disposer = new Rx.Subscription()
return originalUnmount.apply(this, arguments)
}
})
return target
}
@RxAutoDispose
export class BookGridCell extends Component<Props, State> {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment