Skip to content

Instantly share code, notes, and snippets.

@shiftyp
Forked from hswolff/lazyAnalytics.js
Last active April 29, 2020 20:03
Show Gist options
  • Save shiftyp/169b90aae81303fc883698ac9ea26f12 to your computer and use it in GitHub Desktop.
Save shiftyp/169b90aae81303fc883698ac9ea26f12 to your computer and use it in GitHub Desktop.
import { useInstance } from 'object-hooks';
class Analytics {
private api = () => {
const raceError = new Error('handler called before loaded');
bugsnagClient.notify(raceError);
};
public log = (...args: any[]) => {
return this.api(...args);
};
async load() {
this.api = (await import('./lazyAnalytics')).default
}
}
function useLazyAnalytics() {
const [analytics] = useInstance(Analytics);
useEffect(() => {
analytics.load();
}, []);
return analytics.log;
}
// usage
const log = useLazyAnalytics();
// later
log(/* ... call */)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment