Skip to content

Instantly share code, notes, and snippets.

@EyalPerry
Last active June 27, 2020 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyalPerry/81627975b72abe71edf6bbd3804e8bda to your computer and use it in GitHub Desktop.
Save EyalPerry/81627975b72abe71edf6bbd3804e8bda to your computer and use it in GitHub Desktop.
microfrontend
import React {useState, useEffect} from 'react';
import {Switch, Route} from 'react-router-dom';
function Frontend({manifest}){
return <iframe src={manifest.url} />
}
function App() {
const [manifest, setManifest] = useState();
useEffect(() => {
fetch('https://localhost/my-project/manifest').then(r=>r.json()).then(setManifest)
},[]);
if(!manifest){
return <div>Loading</div>
}
return (
<Switch>
{manifest.apps.map(app => <Route to={app.route}><Frontend manifest={app}/></Route>)}
</Switch>
)
}
export default App;
{
"apps":[
{
"id":"user-profile",
"url": "http://localhost:3000/index.js",
"route":"/profile"
},
{
"id":"shop",
"url": "http://localhost:3001/index.js",
"route":"/ship"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment