Last active
January 28, 2019 04:27
-
-
Save developit/fa084f3cb38778f93d58607377f416a3 to your computer and use it in GitHub Desktop.
preact-unrecycle.js
This file contains hidden or 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
/** 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