{"id":8355,"date":"2025-07-28T01:32:26","date_gmt":"2025-07-28T01:32:26","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8355"},"modified":"2025-07-28T01:32:26","modified_gmt":"2025-07-28T01:32:26","slug":"creating-micro-frontends-with-react-8","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-micro-frontends-with-react-8\/","title":{"rendered":"Creating Micro Frontends with React"},"content":{"rendered":"<h1>Creating Micro Frontends with React: A Comprehensive Guide<\/h1>\n<p>In recent years, the concept of micro frontends has gained immense popularity among developers. As applications grow in complexity, traditional monolithic architectures can lead to challenges in development, deployment, and maintenance. Micro frontends break down these issues by enabling teams to build independent, scalable, and manageable front-end applications. In this article, we will 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 concept to front-end development, allowing various teams to work on different parts of an application independently. Each team can select their own stack, deploy independently, and maintain their own part of the codebase without impacting others. This architecture promotes scalability and flexibility, making it easier to innovate and deliver features faster.<\/p>\n<h2>Benefits of Micro Frontends<\/h2>\n<ul>\n<li><strong>Independence:<\/strong> Teams can develop, deploy, and scale services independently.<\/li>\n<li><strong>Technology Diversity:<\/strong> Different parts of the application can use various frameworks and libraries.<\/li>\n<li><strong>Improved Scalability:<\/strong> Teams can focus on their respective areas and address performance optimizations individually.<\/li>\n<li><strong>Enhanced Collaboration:<\/strong> Smaller teams can work more efficiently with less friction.<\/li>\n<\/ul>\n<h2>Setting Up Your Development Environment<\/h2>\n<p>Before diving into the implementation, you\u2019ll need a ready development environment. Ensure you have the following:<\/p>\n<ul>\n<li><strong>Node.js:<\/strong> Install it from <a href=\"https:\/\/nodejs.org\/\">nodejs.org<\/a>.<\/li>\n<li><strong>React:<\/strong> You can bootstrap your React application using Create React App.<\/li>\n<li><strong>Webpack Module Federation:<\/strong> Install the necessary plugins and configurations for integrating micro frontends.<\/li>\n<\/ul>\n<h2>Creating a Micro Frontend Application<\/h2>\n<p>To understand how to create micro frontends, let\u2019s break this down into actionable steps:<\/p>\n<h3>1. Create the Host Application<\/h3>\n<p>Let\u2019s start by creating a host application that will render different micro frontends.<\/p>\n<pre><code>npx create-react-app host-app\ncd host-app\nnpm install --save-dev webpack@5 @module-federation\/webpack-module-federation-plugin\n<\/code><\/pre>\n<p>Edit your webpack configuration to enable module federation. You&#8217;ll need to create a file called <strong>webpack.config.js<\/strong> in the root directory:<\/p>\n<pre><code>const ModuleFederationPlugin = require('webpack\/lib\/container\/ModuleFederationPlugin');\n\nmodule.exports = {\n  \/\/ other configuration... \n  plugins: [\n    new ModuleFederationPlugin({\n      name: 'host',\n      remotes: {\n        app1: 'app1@http:\/\/localhost:3001\/remoteEntry.js',\n        app2: 'app2@http:\/\/localhost:3002\/remoteEntry.js'\n      },\n      shared: { react: { singleton: true }, 'react-dom': { singleton: true } },\n    }),\n  ],\n};\n<\/code><\/pre>\n<h3>2. Create Micro Frontend Applications<\/h3>\n<p>Next, let\u2019s create two micro frontend applications. You can use Create React App again for this purpose:<\/p>\n<pre><code>npx create-react-app app1\ncd app1\nnpm install --save-dev webpack@5 @module-federation\/webpack-module-federation-plugin\n<\/code><\/pre>\n<p>Edit <strong>webpack.config.js<\/strong> in <strong>app1<\/strong>:<\/p>\n<pre><code>const ModuleFederationPlugin = require('webpack\/lib\/container\/ModuleFederationPlugin');\n\nmodule.exports = {\n  \/\/ other configuration... \n  plugins: [\n    new ModuleFederationPlugin({\n      name: 'app1',\n      filename: 'remoteEntry.js',\n      exposes: {\n        '.\/Component1': '.\/src\/Component1', \/\/ Add the component you want to expose\n      },\n      shared: { react: { singleton: true }, 'react-dom': { singleton: true } },\n    }),\n  ],\n};\n<\/code><\/pre>\n<p>Repeat the same process for the second micro frontend, <strong>app2<\/strong>, adjusting the name and exposed components accordingly.<\/p>\n<h3>3. Create Shared Components<\/h3>\n<p>Within each micro frontend application (e.g., app1), create a new component you want to expose:<\/p>\n<pre><code>\/\/ src\/Component1.js\nimport React from 'react';\n\nconst Component1 = () =&gt; {\n  return <div>This is a component from App1<\/div>;\n};\n\nexport default Component1;\n<\/code><\/pre>\n<h3>4. Rendering Micro Frontends in Host Application<\/h3>\n<p>Now, let\u2019s render these components in our host application. Modify the App component in <strong>host-app\/src\/App.js<\/strong> to include:<\/p>\n<pre><code>import React, { useEffect, useState } from 'react';\n\nconst App = () =&gt; {\n  const [Component1, setComponent1] = useState(null);\n  const [Component2, setComponent2] = useState(null);\n\n  useEffect(() =&gt; {\n    const loadComponent1 = async () =&gt; {\n      const { default: Comp1 } = await import('app1\/Component1');\n      setComponent1(() =&gt; Comp1);\n    };\n\n    const loadComponent2 = async () =&gt; {\n      const { default: Comp2 } = await import('app2\/Component2');\n      setComponent2(() =&gt; Comp2);\n    };\n\n    loadComponent1();\n    loadComponent2();\n  }, []);\n\n  return (\n    <div>\n      <h1>Host Application<\/h1>\n      {Component1 &amp;&amp; }\n      {Component2 &amp;&amp; }\n    <\/div>\n  );\n};\n\nexport default App;\n<\/code><\/pre>\n<h2>Deployment Strategies<\/h2>\n<p>Micro frontend applications can be deployed in a variety of ways. Here are some common options:<\/p>\n<ul>\n<li><strong>Independent Deployments:<\/strong> Each micro frontend can be deployed independently on different platforms.<\/li>\n<li><strong>Containerized Deployments:<\/strong> Use Docker containers to package your micro frontends and deploy them through orchestration platforms like Kubernetes.<\/li>\n<li><strong>CDN Distribution:<\/strong> Host your micro frontends on a Content Delivery Network (CDN) for faster load times and better global access.<\/li>\n<\/ul>\n<h2>Best Practices for Micro Frontends<\/h2>\n<ul>\n<li><strong>Version Control:<\/strong> Use semantic versioning to manage shared components and dependencies effectively.<\/li>\n<li><strong>Consistent Styling:<\/strong> Ensure a cohesive user experience by unifying styles across micro frontends.<\/li>\n<li><strong>Effective Communication:<\/strong> Use global event bus or shared state management for seamless interactions between micro frontends.<\/li>\n<li><strong>Monitoring and Logging:<\/strong> Implement monitoring and logging mechanisms to track the performance of each micro frontend.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Micro frontends can vastly improve the development processes for larger applications by promoting modularity, scalability, and flexibility. By leveraging tools like React and Webpack&#8217;s Module Federation, teams can create powerful micro frontend architectures that can evolve independently. As web applications continue to grow, adopting micro frontends will provide the agility necessary to meet modern development challenges.<\/p>\n<p>As you embark on creating your micro frontend projects, remember to keep best practices in mind and continuously iterate based on feedback and performance insights. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Micro Frontends with React: A Comprehensive Guide In recent years, the concept of micro frontends has gained immense popularity among developers. As applications grow in complexity, traditional monolithic architectures can lead to challenges in development, deployment, and maintenance. Micro frontends break down these issues by enabling teams to build independent, scalable, and manageable front-end<\/p>\n","protected":false},"author":100,"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":["post-8355","post","type-post","status-publish","format-standard","category-react","tag-react"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8355","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\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8355"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8355\/revisions"}],"predecessor-version":[{"id":8356,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8355\/revisions\/8356"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}