Building Micro Frontends With React

Kunalznk
4 min readJun 12, 2023

Unlocking the Power of Micro Applications with React: A Step-by-Step Guide

In the last section, I provided a high-level overview of what Micro Frontends are. Now, in this post, we will dive deeper and build an independent Micro Frontend application using React.

Let’s consider our monolithic e-commerce application, which includes the following pages for user interaction:
- Product search
- Product details
- Purchase flow
- Order monitoring
- Delivery tracking

Microfrontend

In the previous section, I briefly touched upon the integration of these Micro Frontend applications. Now, we will discuss the integration methods in more detail. There are mainly three methods we can follow for integration: server-side integration, client-side integration, and build-time integration.

Server-side integration involves integrating the frontend and server backend application on the server side and sending it to the client. This approach can impose a significant overhead on the server.

Build-time integration, on the other hand, allows integration to occur during the build process. For example, when building the main application, we include all the Micro Frontend apps. One way to achieve this is by publishing each Micro Frontend application as an npm package and using them in the main application. This way, all the Micro Frontend applications will be built along with the main application.

For our Micro Frontend, we will be using Runtime integration. This means that the required Micro Frontend applications will be downloaded on the client and integrated at runtime. By doing this, we can reduce the initial load time. To achieve runtime integration, we will use Webpack 5 with Module Federation.

Now, let’s build our three Micro Frontend applications using the `create-mf-app` tool. This tool sets up a Create React App with minimal configuration, including Webpack and React.

To create a Micro Frontend application, run the following command:

When prompted, provide the following information:
- Pick the name of your app: “product” // name of the app
- Project Type: Application
- Port number: 9001
- Framework: React
- Language: JavaScript
- CSS: Tailwind

After providing the above details, your ‘product’ project will be ready to go.

In the Product app, we will have two pages: “/products” for searching products and “/product/prodId” for displaying product details. To begin, let’s install the necessary dependencies, axios and react-router-dom, using the following command:

npm i axios react-router-dom

Next, we need to add routing to our application. Here, the basename prop will be considered as the root route, meaning all links will start with "/products". This ensures that our application is rendered at "/products" and not at "/".

App.jsx
Bootstrap.jsx

To view the complete code for the Product Micro App, you can check it out here

The last step for our Product Micro App is to expose or share some components that can be used by other apps and the main application. To do this, we need to make a modification in the src/webpack.config.js file. Add the following lines to expose the "product" module:

exposed product module

With this change, we have successfully completed the Product Micro App. You can view the code for the other Micro Apps here

Before we proceed to the next post, please make sure to gather the entire code. I apologize for skipping as it would have made the blog post excessively long, and I wouldn’t want you to fall asleep out of frustration! 😫😪

In the next post, we will integrate all the Micro Applications into one. For now, I encourage you to explore the other Micro Apps, which are similar to React. If you don’t understand them fully, don’t worry, I assure you that it will become clearer in the next post.

Let’s keep the interaction going! Feel free to share your thoughts and questions.

in the next post will integrate all micro application into one i live to you guys to understand other micro app which is similar to react and if you dont understand i assure you will understand in next post

--

--