Last active
December 9, 2017 12:02
-
-
Save daniellandau/a308334d5b591e2c45c128bb9f463ea9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// related: https://twitter.com/andrestaltz/status/939257877600104448 | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
class Foobar extends React.Component { | |
componentDidMount() { | |
console.log('Did mount'); | |
} | |
shouldComponentUpdate(newProps) { | |
return false; | |
} | |
render() { | |
return <h1>{`${new Date()}`}</h1>; | |
} | |
} | |
function withPeriodicRefresh(cls) { | |
function NewClass () {} | |
let interval; | |
NewClass.prototype = Object.create(cls.prototype); | |
NewClass.prototype.componentDidMount = function() { | |
interval = setInterval(() => this.forceUpdate(), 1e3); | |
typeof cls.prototype.componentDidMount == 'function' | |
&& cls.prototype.componentDidMount.call(this); | |
} | |
NewClass.prototype.componentWillUnmount = function() { | |
clearInterval(interval); | |
typeof cls.prototype.componentWillUnmount === 'function' | |
&& cls.prototype.componentWillUnmount.call(this); | |
} | |
return NewClass; | |
} | |
const Foobar2 = withPeriodicRefresh(Foobar); | |
ReactDOM.render( | |
<Foobar2 />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment