{"id":7399,"date":"2025-06-29T13:32:30","date_gmt":"2025-06-29T13:32:29","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7399"},"modified":"2025-06-29T13:32:30","modified_gmt":"2025-06-29T13:32:29","slug":"creating-micro-frontends-with-react-5","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/creating-micro-frontends-with-react-5\/","title":{"rendered":"Creating Micro Frontends with React"},"content":{"rendered":"<h1>Creating Micro Frontends with React<\/h1>\n<p>In the ever-evolving landscape of web development, the rise of micro frontends has redefined how we build and manage complex applications. This architectural style allows teams to work independently, improving scalability and flexibility. In this article, we\u2019ll explore how to create micro frontends using React, covering key concepts, best practices, and an example implementation.<\/p>\n<h2>What Are Micro Frontends?<\/h2>\n<p>Micro frontends extend the principles of microservices to the frontend world. Instead of creating a single, monolithic application, micro frontends enable developers to break the frontend into smaller, manageable pieces. Each piece can be developed, tested, deployed, and scaled independently by various teams.<\/p>\n<p>This approach provides several benefits:<\/p>\n<ul>\n<li><strong>Independence:<\/strong> Teams can use different technologies, frameworks, or versions based on their needs.<\/li>\n<li><strong>Scalability:<\/strong> Different parts of the application can be scaled separately, optimizing resource allocation.<\/li>\n<li><strong>Improved Collaboration:<\/strong> Smaller codebases result in easier collaboration among team members.<\/li>\n<\/ul>\n<h2>Understanding React in the Micro Frontend Context<\/h2>\n<p>React is an excellent choice for building micro frontends due to its component-based architecture, which aligns perfectly with the micro frontend philosophy. React components can be independently developed, making integration smooth and efficient.<\/p>\n<h3>Core React Features for Micro Frontends<\/h3>\n<p>Here are some features of React that support micro frontend development:<\/p>\n<ul>\n<li><strong>Reusable Components:<\/strong> Components can be shared across different micro frontends, allowing for consistent UI and functionality.<\/li>\n<li><strong>State Management:<\/strong> Libraries like Redux or Context API can manage state effectively across micro frontends.<\/li>\n<li><strong>Lazy Loading:<\/strong> React&#8217;s dynamic import capabilities allow portions of the application to be loaded only when needed, optimizing performance.<\/li>\n<\/ul>\n<h2>Setting Up Your Micro Frontend Architecture<\/h2>\n<p>Creating a micro frontend architecture with React involves several steps. Here, we present a high-level overview of the architecture and technical stack needed.<\/p>\n<h3>1. Choose Your Deployment Strategy<\/h3>\n<p>There are several strategies to deploy micro frontends, including:<\/p>\n<ul>\n<li><strong>Server-Side Composition:<\/strong> Assemble the micro frontends at the server and serve a complete HTML page to the client.<\/li>\n<li><strong>Client-Side Composition:<\/strong> Load micro frontends dynamically on the client-side, allowing for a more flexible structure.<\/li>\n<\/ul>\n<h3>2. Set Up Module Federation<\/h3>\n<p>Webpack 5 introduced Module Federation, which is vital for micro frontends. This feature allows you to load code from the remote server dynamically. Here\u2019s how to use it:<\/p>\n<pre><code>const moduleFederationConfig = {\n  name: \"app1\",\n  remotes: {\n    app2: \"app2@http:\/\/localhost:3002\/remoteEntry.js\",\n  },\n  shared: {\n    react: { singleton: true, requiredVersion: \"^17.0.2\" },\n    \"react-dom\": { singleton: true, requiredVersion: \"^17.0.2\" },\n  },\n};\n<\/code><\/pre>\n<p>In this example, <code>app1<\/code> can use components from <code>app2<\/code> seamlessly.<\/p>\n<h3>3. Set Up React Applications<\/h3>\n<p>Each micro frontend will be a separate React application. Here\u2019s a basic structure for one micro frontend leveraging Create React App:<\/p>\n<pre><code>npx create-react-app app1\ncd app1\nnpm install --save react-router-dom\n<\/code><\/pre>\n<h2>Building Your First Micro Frontend<\/h2>\n<p>Let&#8217;s build our first simple micro frontend. We&#8217;ll create two micro frontends: <strong>App One<\/strong> and <strong>App Two<\/strong>. App One will consume a component from App Two.<\/p>\n<h3>Creating App One<\/h3>\n<p>Go to your project directory and create App One:<\/p>\n<pre><code>npx create-react-app app1\ncd app1\n<\/code><\/pre>\n<p>Next, modify the <code>src\/App.js<\/code> file:<\/p>\n<pre><code>import React from \"react\";\n\nfunction App() {\n  return (\n    <div>\n      <h1>Micro Frontend: App One<\/h1>\n    <\/div>\n  );\n}\n\nexport default App;<\/code><\/pre>\n<h3>Creating App Two<\/h3>\n<p>Now, let\u2019s create the second micro frontend:<\/p>\n<pre><code>npx create-react-app app2\ncd app2\n<\/code><\/pre>\n<p>Edit the <code>src\/index.js<\/code> file in App Two:<\/p>\n<pre><code>import(\".\/bootstrap\");<\/code><\/pre>\n<p>Then, create a new file called <code>bootstrap.js<\/code> in the <code>src<\/code> directory:<\/p>\n<pre><code>import React from \"react\";\nimport ReactDOM from \"react-dom\";\nimport App from \".\/App\";\n\nconst domElement = document.getElementById(\"app2\");\n\nif (domElement) {\n  ReactDOM.render(, domElement);\n}<\/code><\/pre>\n<p>Finally, modify <code>src\/App.js<\/code> for App Two:<\/p>\n<pre><code>import React from \"react\";\n\nfunction App() {\n  return (\n    <div>\n      <h1>Micro Frontend: App Two<\/h1>\n    <\/div>\n  );\n}\n\nexport default App;<\/code><\/pre>\n<h3>Configuring Webpack for Module Federation<\/h3>\n<p>Configure each application&#8217;s Webpack settings to use Module Federation. Here\u2019s an example of what your webpack configuration would look like for <code>app1<\/code>:<\/p>\n<pre><code>const ModuleFederationPlugin = require(\"webpack\/lib\/container\/ModuleFederationPlugin\");\n\nmodule.exports = {\n  \/\/... rest of your configuration\n  plugins: [\n    new ModuleFederationPlugin({\n      name: \"app1\",\n      filename: \"remoteEntry.js\",\n      exposes: {\n        \".\/App\": \".\/src\/App\", \/\/ Expose your primary component\n      }\n    }),\n  ],\n};\n<\/code><\/pre>\n<h2>Integrating Micro Frontends<\/h2>\n<p>Integration is key to making micro frontends work smoothly. You can do this via client-side routing or through a shared layout:<\/p>\n<h3>Using React Router<\/h3>\n<p>To navigate between micro frontends, use React Router. Here\u2019s how you can implement routing:<\/p>\n<pre><code>import { BrowserRouter as Router, Route, Switch } from \"react-router-dom\";\nimport AppOne from \".\/AppOne\";\nimport AppTwo from \"app2\/App\"; \/\/ Importing the remote app\n\nfunction Main() {\n  return (\n    \n      \n        \n        \n      \n    \n  );\n}\n<\/code><\/pre>\n<h3>Handling Shared State<\/h3>\n<p>Using Context API or third-party state management tools can help manage shared state:<\/p>\n<pre><code>import React, { createContext, useContext, useState } from \"react\";\n\nconst SharedStateContext = createContext();\n\nexport const SharedStateProvider = ({ children }) =&gt; {\n  const [sharedState, setSharedState] = useState(\"Initial State\");\n  return (\n    \n      {children}\n    \n  );\n};\n\nexport const useSharedState = () =&gt; useContext(SharedStateContext);\n<\/code><\/pre>\n<h2>Testing and Monitoring<\/h2>\n<p>Testing micro frontends individually as well as within the ecosystem is crucial. Use tools like Jest, react-testing-library, or Cypress for integration testing.<\/p>\n<h3>Performance Monitoring<\/h3>\n<p>Consider setting up performance monitoring and error tracking using tools like Sentry or Google Analytics to detect and resolve issues quickly across micro frontends.<\/p>\n<h2>Best Practices for Micro Frontends<\/h2>\n<p>To ensure success with your micro frontend architecture, adhere to the following best practices:<\/p>\n<ul>\n<li><strong>Maintain Component Library:<\/strong> Create a shared component library to ensure UI consistency.<\/li>\n<li><strong>Automate Tests:<\/strong> Implement CI\/CD pipelines for automated testing and deployment.<\/li>\n<li><strong>Documentation:<\/strong> Keep documentations updated to help teams collaborate effectively across different frontend microservices.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Micro frontends using React offer a powerful approach to building scalable, maintainable applications. By leveraging React&#8217;s component architecture, teams can work independently on different parts of the application, fostering better collaboration and development agility. Take the first step towards adopting micro frontends in your projects by implementing the steps outlined in this article, and watch your development workflow transform for the better!<\/p>\n<p>As the trend of micro frontends continues to grow, understanding and implementing this architecture will give you a competitive edge in the web development landscape. Dive in, experiment, and enjoy the benefits of micro frontends!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating Micro Frontends with React In the ever-evolving landscape of web development, the rise of micro frontends has redefined how we build and manage complex applications. This architectural style allows teams to work independently, improving scalability and flexibility. In this article, we\u2019ll explore how to create micro frontends using React, covering key concepts, best practices,<\/p>\n","protected":false},"author":85,"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-7399","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\/7399","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\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7399"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7399\/revisions"}],"predecessor-version":[{"id":7400,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7399\/revisions\/7400"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}