Closed arunreddy-kr closed 3 years ago
Hey,
Personnaly I wrapped my Microfrontend inside an error boundary : https://fr.reactjs.org/docs/error-boundaries.html In the host App, I load asyncronously the Micro-frontend. If it fails I display an Error component.
ErrorBoundary.jsx
import React from 'react';
import NotFound from 'components/pages/NotFound';
import Loading from 'components/layout/Loading';
const AsyncLoader = ({ children }) => {
return (
<ErrorBoundary>
<React.Suspense fallback={<Loading />}>
{children}
</React.Suspense>
</ErrorBoundary>
)
};
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
return this.state.hasError ? <NotFound /> : this.props.children;
}
}
export default AsyncLoader;
Host App :
const MyMicrofrontEnd = loadable(() => import('myMicrofrontend/App'), { fallback: <Loading /> });
const App = () => (
[...]
<Route exact path='/mfe'>
<ErrorBoundary>
<MyMicrofrontEnd />
</ErrorBoundary>
</Route>
[...]
);
@ChrisHendrX May I know where did you get the "loadable" function?
https://www.npmjs.com/package/@loadable/component
It's an alternative of React.lazy
If you don't want to use loadable, you can simply import your component with React lazy this way :
const MyMicrofrontEnd = React.lazy(() => import("myMicrofrontend/App"))
@ChrisHendrX Thanks for pointing me to the @loadable package. I am not seeing AsyncLoader is being used anywhere in your example. What is the use of it? Also, I have the following code in my App.tsx file. Could you let me know how to modify it?
import React, {lazy, Suspense} from 'react';
import ContainerMenu from './components/ContainerMenu/ContainerMenu';
import {ErrorBoundary} from 'react-error-boundary';
import './App.css';
import {Redirect, Route, Switch, Router} from 'react-router-dom';
const MF1AppLazy = lazy(() => import('./components/MF1App/MF1'));
const MF2AppLazy = lazy(() => import('./components/MF2App/MF2'));
const App = () => {
return (
<div className="container">
<ContainerMenu></ContainerMenu>
<div className="components">
<Suspense fallback={<div>loading...</div>}>
<Switch>
<Route path="/" exact>
<Redirect to="/mf1"></Redirect>
</Route>
<Route path="/mf1" component={MF1AppLazy}></Route>
<Route path="/mf2" exact component={MF2AppLazy} />
</Switch>
</Suspense>
</div>
</div>
);
};
export default App;
@ChrisHendrX Could you guide me on the usage of AsyncLoader code?
Hey,
I think I gave you the correct code 🤓
ErrorBoundary.jsx import React from 'react';
const NotFound = () => (
);
const Loading = () => (
Loading the remote component...
) const AsyncLoader = ({ children }) => { return (
) };
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; }
static getDerivedStateFromError(error) { return { hasError: true }; }
render() {
return this.state.hasError ?
export default AsyncLoader;
App.jsx import React from "react"; import { BrowserRouter, Switch, Route } from "react-router-dom"; import AsyncLoader from "the/path/to/ErrorBoundary"; const MyRemoteComponent = React.lazy(() => import("myRemoteComponent/App")); const App = () => {
It worked. Thanks for guiding me. Appreciate the help
I have a container and two micro frontends developed in react and typescript. I am trying to make container up and running if one of the microfrontend is down. Is there any way to do that? I didn't see that feature being available in the module federation