Skip to content

Instantly share code, notes, and snippets.

@mfrachet
Created July 26, 2019 17:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfrachet/a04cc57a500de85170e2ade4b9406305 to your computer and use it in GitHub Desktop.
Save mfrachet/a04cc57a500de85170e2ade4b9406305 to your computer and use it in GitHub Desktop.
React lazy implementation
import React from "react";
let IDS = 0;
const loaded = {};
export const lazy = modulePathResolver => {
const id = IDS++;
return props => {
const LoadedComponent = loaded[id];
if (LoadedComponent) {
return <LoadedComponent {...props} />;
}
throw modulePathResolver().then(lazyModule => {
const Component = lazyModule.default;
loaded[id] = Component;
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment