-
-
Save ooade/2c6405cb672792fa02e07e884dd4eb44 to your computer and use it in GitHub Desktop.
A couple of variants for preact split points
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
import { h, Component } from 'preact'; | |
/** | |
* <SplitPoint load={require('bundle?lazy!./foo')} fallbackContent={( | |
* <div class="loading" /> | |
* )} /> | |
*/ | |
class SplitPoint extends Component { | |
componentWillMount() { | |
let cb = this.linkState('child'), | |
r = this.props.load(cb); | |
if (r.then) r.then(cb); | |
} | |
render({ load, fallbackContent, ...props }, { child }) { | |
return child ? h(child, props) : fallbackContent || null; | |
} | |
} | |
class SplitPoint extends Component { | |
componentWillMount() { | |
let cb = this.linkState('child'), | |
r = this.props.load(); | |
r.then ? r.then(cb) : cb(r); | |
} | |
render(props, { child }) { | |
return child ? h(child, props) : null; | |
} | |
} | |
class Split extends Component { | |
render({ load, ...props }, { child }) { | |
if (!child && !this.child) { | |
let c = this.child = load(); | |
c.then ? c.then(this.linkState('child')) : (child = c); | |
} | |
return child ? h(child, props) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment