Skip to content

Instantly share code, notes, and snippets.

@daniellandau
Last active December 9, 2017 12:02
Show Gist options
  • Save daniellandau/a308334d5b591e2c45c128bb9f463ea9 to your computer and use it in GitHub Desktop.
Save daniellandau/a308334d5b591e2c45c128bb9f463ea9 to your computer and use it in GitHub Desktop.
// 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