Skip to content

Instantly share code, notes, and snippets.

@kafkahw
Created June 5, 2017 23:30
Show Gist options
  • Save kafkahw/0a4171f670c4935825d611e6da5598ea to your computer and use it in GitHub Desktop.
Save kafkahw/0a4171f670c4935825d611e6da5598ea to your computer and use it in GitHub Desktop.
Remote react component loading
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