core / Variable

useBrowserOnly

Dependency Assembler that assembles a function, which takes another function, invokes it and forwards its return value if within a browser environment, or return null otherwise.

Presentation

const useBrowserOnly: (platform?: Object) => <T>(fn: () => T) => T | null;

Example usage

private browserOnly = useBrowserOnly();
// href is `null` if not in a browser environment
const href = this.browserOnly(() => window.location.href);
// alert is only invoked in a browser environment
this.browserOnly(() => alert('Hello, World!'));