Created
June 5, 2017 23:30
-
-
Save kafkahw/0a4171f670c4935825d611e6da5598ea to your computer and use it in GitHub Desktop.
Remote react component loading
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'; | |
export function loadRemoteComponent(url) { | |
return fetch(url) | |
.then(response => response.text()) | |
.then(text => { | |
// Define exports and require method for eval(text) | |
const exports = {}; | |
function require(name) { | |
if (name === 'react') { | |
return React; | |
} | |
throw new Error('Does not support modules other than "react" in remote component'); | |
} | |
eval(text); | |
return exports.__esModule ? exports.default : exports; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment