Skip to content

Instantly share code, notes, and snippets.

@eps1lon
Created March 14, 2019 11:46
Show Gist options
  • Save eps1lon/adbf9e6799dcb1d056f07733f177cb13 to your computer and use it in GitHub Desktop.
Save eps1lon/adbf9e6799dcb1d056f07733f177cb13 to your computer and use it in GitHub Desktop.
React lazy + forwardRef
const Component = React.lazy(() => {
return import("./Component").then(({ default: Component }) => {
return {
default: React.forwardRef((props, ref) => (
<Component ref={ref} {...props} />
))
};
});
});
// as a function
function lazyForwardRef(factory) {
return () => factory().then(({ default: Component }) => {
return {
default: React.forwardRef((props, ref) => (
<Component ref={ref} {...props} />
))
};
});
}
const SomeLazyComponent = lazyForwardRef(() => import('/path/to/Component'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment