{"id":7176,"date":"2025-06-23T01:32:25","date_gmt":"2025-06-23T01:32:25","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7176"},"modified":"2025-06-23T01:32:25","modified_gmt":"2025-06-23T01:32:25","slug":"creating-micro-frontends-with-react-4","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-micro-frontends-with-react-4\/","title":{"rendered":"Creating Micro Frontends with React"},"content":{"rendered":"<h1>Creating Micro Frontends with React<\/h1>\n<p>As web applications grow in complexity, so does the need for efficient architecture that allows teams to work simultaneously without stepping on each other&#8217;s toes. Enter micro frontends: a paradigm shift in frontend development that breaks up monolithic applications into smaller, manageable pieces. In this blog, we&#8217;ll explore how to create micro frontends using React, a popular JavaScript library for building user interfaces.<\/p>\n<h2>What Are Micro Frontends?<\/h2>\n<p>Micro frontends extend the microservices approach to the frontend world. Instead of one large application, micro frontends involve multiple self-contained applications (or &#8220;micro frontends&#8221;) that can be developed, tested, and deployed independently. Each team can choose its own technologies, allowing greater flexibility and scalability.<\/p>\n<h2>Benefits of Using Micro Frontends<\/h2>\n<p>The micro frontend architecture offers several advantages:<\/p>\n<ul>\n<li><strong>Scalability:<\/strong> Teams can add or update functionalities independently without affecting the entire application.<\/li>\n<li><strong>Flexibility:<\/strong> Different teams can use different tools and libraries, making it easier to adopt new technologies.<\/li>\n<li><strong>Improved Deployment:<\/strong> Smaller applications can be deployed more frequently and with less risk of breaking the entire system.<\/li>\n<li><strong>Enhanced team autonomy:<\/strong> Teams can operate independently, which can speed up development and reduce dependencies.<\/li>\n<\/ul>\n<h2>Challenges of Micro Frontends<\/h2>\n<p>Despite their advantages, micro frontends come with their own set of challenges:<\/p>\n<ul>\n<li><strong>Integration:<\/strong> Combining different frontend technologies can lead to inconsistencies.<\/li>\n<li><strong>Complex Build Process:<\/strong> Managing the lifecycle and dependencies of multiple applications can be complex.<\/li>\n<li><strong>Performance:<\/strong> Loading multiple micro frontends can contribute to slower performance if not handled correctly.<\/li>\n<\/ul>\n<h2>Getting Started with Micro Frontends and React<\/h2>\n<p>To create a micro frontend using React, we can use a few modern techniques and tools. The following steps illustrate how to create a simple micro frontend system:<\/p>\n<h3>1. Project Setup<\/h3>\n<p>Start by setting up a new React application for your micro frontend. You can use Create React App (CRA) for this purpose. Run the following command:<\/p>\n<pre>\n<code>npx create-react-app my-micro-frontend<\/code>\n<\/pre>\n<p>Once your project is set up, navigate into the project directory:<\/p>\n<pre>\n<code>cd my-micro-frontend<\/code>\n<\/pre>\n<h3>2. Define Your Micro Frontends<\/h3>\n<p>Let\u2019s assume we are creating a simple e-commerce web application with two micro frontends:<\/p>\n<ul>\n<li>Shopping Cart<\/li>\n<li>Product Catalog<\/li>\n<\/ul>\n<p>Create separate directories for your micro frontends:<\/p>\n<pre>\n<code>mkdir shopping-cart product-catalog<\/code>\n<\/pre>\n<p>Now, initialize a new React application in each of these directories:<\/p>\n<pre>\n<code>\ncd shopping-cart\nnpx create-react-app .\ncd ..\/product-catalog\nnpx create-react-app .\n<\/code>\n<\/pre>\n<h3>3. Building the Micro Frontends<\/h3>\n<p>Let\u2019s add simple components to each micro frontend application.<\/p>\n<h4>Shopping Cart Micro Frontend<\/h4>\n<p>Open the `shopping-cart\/src\/App.js` file and replace its contents with the following:<\/p>\n<pre>\n<code>\nimport React from 'react';\n\nfunction App() {\n    return (\n        <div>\n            <h1>Shopping Cart<\/h1>\n            <p>Your items will appear here.<\/p>\n        <\/div>\n    );\n}\n\nexport default App;\n<\/code>\n<\/pre>\n<h4>Product Catalog Micro Frontend<\/h4>\n<p>Next, update the `product-catalog\/src\/App.js` file with this content:<\/p>\n<pre>\n<code>\nimport React from 'react';\n\nfunction App() {\n    return (\n        <div>\n            <h1>Product Catalog<\/h1>\n            <p>List of products goes here.<\/p>\n        <\/div>\n    );\n}\n\nexport default App;\n<\/code>\n<\/pre>\n<h3>4. Combining Micro Frontends<\/h3>\n<p>To integrate these micro frontends into a single application, we need a shell application. This will load the micro frontends dynamically. Create another React app in a new directory:<\/p>\n<pre>\n<code>npx create-react-app shell-app<\/code>\n<\/pre>\n<p>In your shell application, install a library for importing applications remotely. We\u2019ll use an npm package called &#8220;single-spa-react&#8221;:<\/p>\n<pre>\n<code>npm install single-spa-react<\/code>\n<\/pre>\n<p>Next, create a simple setup in the `shell-app` to render your micro frontends. Open the `src\/index.js` file and add the following code:<\/p>\n<pre>\n<code>\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { registerApplication, start } from 'single-spa';\nimport { mountRootParcel } from 'single-spa-react';\n\nconst loadApp = (name) =&gt; () =&gt; import(`http:\/\/localhost:3000\/${name}\/main.js`);\n\nregisterApplication('shopping-cart', loadApp('shopping-cart'), location =&gt; location.pathname.startsWith('\/cart'));\nregisterApplication('product-catalog', loadApp('product-catalog'), location =&gt; location.pathname.startsWith('\/products'));\n\nstart();\n\nReactDOM.render(<div id=\"app\">Micro Frontend Shell<\/div>, document.getElementById('app'));\n<\/code>\n<\/pre>\n<h3>5. Serving the Micro Frontends<\/h3>\n<p>To serve each application, you can use a simple HTTP server. A common choice is `http-server`, which can be installed globally:<\/p>\n<pre>\n<code>npm install -g http-server<\/code>\n<\/pre>\n<p>Run this command in each micro frontend\u2019s directory to serve them:<\/p>\n<pre>\n<code>http-server build -p 3000<\/code>\n<\/pre>\n<p>Replace `3000` with different port numbers for each micro frontend to avoid conflicts.<\/p>\n<h3>6. Navigating Between Micro Frontends<\/h3>\n<p>You can navigate to your micro frontends through respective URLs. For example:<\/p>\n<ul>\n<li><a href=\"http:\/\/localhost:3000\/cart\">Shopping Cart<\/a><\/li>\n<li><a href=\"http:\/\/localhost:3000\/products\">Product Catalog<\/a><\/li>\n<\/ul>\n<p>When you navigate to these URLs, you should see the respective micro frontends render their content correctly.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this blog, we explored how to create micro frontends using React, utilizing both separate application architectures and a shell application to bring them together. Micro frontends allow teams to build and deploy applications independently, leading to increased productivity and flexibility in technology choices. While micro frontends may introduce some complexity in integration and management, the benefits they bring to large-scale applications are worth considering.<\/p>\n<p>As you embark on your journey with micro frontends, remember to assess your team\u2019s needs, goals, and the expected scale of the application to decide if this architecture is the right fit for you. Happy coding!<\/p>\n<h2>Further Reading<\/h2>\n<p>If you&#8217;re interested in diving deeper into micro frontends or related architecture patterns, consider these resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/micro-frontends.org\/\">Micro Frontends &#8211; official website<\/a><\/li>\n<li><a href=\"https:\/\/single-spa.js.org\/\">Single SPA &#8211; official documentation<\/a><\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\">React &#8211; official documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Creating Micro Frontends with React As web applications grow in complexity, so does the need for efficient architecture that allows teams to work simultaneously without stepping on each other&#8217;s toes. Enter micro frontends: a paradigm shift in frontend development that breaks up monolithic applications into smaller, manageable pieces. In this blog, we&#8217;ll explore how to<\/p>\n","protected":false},"author":84,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[398],"tags":[224],"class_list":{"0":"post-7176","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-react","7":"tag-react"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/84"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7176"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7176\/revisions"}],"predecessor-version":[{"id":7177,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7176\/revisions\/7177"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}