Created
July 26, 2019 17:41
-
-
Save mfrachet/a04cc57a500de85170e2ade4b9406305 to your computer and use it in GitHub Desktop.
React lazy implementation
This file contains 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 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