Skip to content

Instantly share code, notes, and snippets.

@kamicane
Last active February 28, 2017 16:13
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 kamicane/e6a434ff12e623978aba4d0d3d5fab33 to your computer and use it in GitHub Desktop.
Save kamicane/e6a434ff12e623978aba4d0d3d5fab33 to your computer and use it in GitHub Desktop.
import { Component } from 'react'
import { observe } from '@nx-js/observer-util'
export default class ObserverComponent extends Component {
componentWillMount () {
const render = this.render
const initialRender = (...args) => {
let result
this.signal = observe(() => {
if (this.render === initialRender) {
result = render.apply(this, args)
} else {
this.forceUpdate()
}
})
this.render = render
return result
}
this.render = initialRender
}
componentWillUnmount () {
this.signal.unobserve()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment