-
-
Save shiftyp/169b90aae81303fc883698ac9ea26f12 to your computer and use it in GitHub Desktop.
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 { 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