Created
July 26, 2019 17:41
Star
You must be signed in to star a gist
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