Skip to content

Instantly share code, notes, and snippets.

@ooade
Forked from developit/split-point.js
Created January 17, 2017 09:45
Show Gist options
  • Save ooade/2c6405cb672792fa02e07e884dd4eb44 to your computer and use it in GitHub Desktop.
Save ooade/2c6405cb672792fa02e07e884dd4eb44 to your computer and use it in GitHub Desktop.
A couple of variants for preact split points
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