Skip to content

Instantly share code, notes, and snippets.

@developit
Last active January 28, 2019 04:27
Show Gist options
  • Save developit/fa084f3cb38778f93d58607377f416a3 to your computer and use it in GitHub Desktop.
Save developit/fa084f3cb38778f93d58607377f416a3 to your computer and use it in GitHub Desktop.
preact-unrecycle.js
/** Usage:
* const MyView = unrecycle(props => {
* // this is normally a no-go and creates leaking styles, but it won't with unrecycle():
* <input ref={ c => c && c.style.background='red' } />
* })
*/
export default function unrecycle(Component) {
return function Unrecycle(props, context) {
this.componentWillUnmount = dontRecycle;
return Component.call(this, props, context);
};
}
function dontRecycle() {
// wait 1 tick for the recycler to reclaim this component, then destroy its cached DOM tree
Promise.resolve(this).then(clearBase);
}
function clearBase(component) {
// Note: __b is the compressed name (it's always this in a prod build, but nextBase is not a public API)
component.nextBase = component.__b = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment