Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Building React Native Micro Frontends

Sign upSign InSign upSign InRavidu PereraFollowBits and Pieces--ListenShareIn modern Application development, the concept of microfrontends has gained considerable traction. As development teams strive for improved scalability, maintainability, and flexibility, Microfrontends offer an intriguing solution to these challenges.Microfrontend frameworks allow you to create smaller and independent units for your frontend application that lets you focus on developing independent parts of your application as components. These small units can be developed, tested, versioned and deployed by separate teams. This will help you to enhance your parallel work and minimize the risk of bottlenecks in the development pipelines.To make things better, you can leverage tools like Bit to simplify the process of building such microfrontends. You don’t have to worry about module federation, dependency management, isolated test environments as Bit takes care of all of that while letting you focus on your business codeSo, let’s look at how you can build a micro frontend-based app using a very popular tool — Bit.Disclaimer: This article assumes that you have a fair understanding of Bit, Scopes, Components, and Component-Driven Development. If you’re relatively new to this field, I recommend going through the Bit documentation and then returning to this article.You need to install the prerequisites before diving into the tutorial.Let’s make sure you have Bit installed in your local development environment. Bit allows you to create, isolate, and share individual components.To install Bit, run the following command:To confirm the installation, run the command:If you see the output shown above, you’ve successfully installed Bit.Next, head over to Bit Cloud and create an account. Next, create a scope as well.You can simply create the scope by selecting either Personal or Team. Press ‘Create’ to finish the creation, and you will be redirected to the scope window.We are currently in the planning phase of developing a microfrontend-based application using React-Native.Our aim is to create a microfrontend focusing on a specific functionality.To achieve this, we will be developing composable components using Bit. These components will be combined within an independent React Native runtime called the App component. To make things simple, we’ll be focusing on implementing a simple microfrontend that showcases the following:As shown below, this will be the final output of the project.First, you need to create a workspace in Bit to develop your React-Native micro frontends.Lets run the below command to create a workspace for your React native application :When you run above command, replace workspace-name and my-org.my-scope with your values. In my case I used react-native-blog as the name for the workspace and the shehan97105.my-demo-scope value, which I defined in the earlier step.Now, go to your workspace and you can start the development server usingbit start the command. You will be redirected to http://localhost:3000 to launch your local development environment.We have completed our initial setup of creating scope and workspace. Now, we have to decide how many components we need to create our micro frontend application. In this micro frontend app, we are planning to build the following components:We will be using separate namespaces to store the above components.Let’s create our first component, ‘Article’.Now, your folder structure is created for the Article component. Navigate through the blog/article/article.tsx file and include the below code.This component renders a single article in our blog. When a user clicks an article tile, this will display the content of the article. This will behave as a building block to display individual articles. It accepts id, title, descriptionand showFullContentprops. Using showFullContentprop, it will toggle between showing a preview of the article and its content. This component is designed as a reusable component for both article-list and Blogcomponents.Now create a article-list component to render the list of articles.As we already used, you can simply create a new component using bit createcommand.Now add below code snippet in your blog/article-list/article-list.tsx file.article-list component renders the list of articles in the blog. The article-list component receives an onArticlePressfunction prop, which is triggered when an article is pressed. Each article entry uses the Articlecomponent to display a preview of the article.This component is meant to be used as a standalone list of articles and is customizable through its props.Now we need to create a Blog component, which acts as the main entry point for the App. Let’s usebit create react-native blog/blog command to create the blog component.After your component gets created, navigate through blog/blog/blog.tsx and add below code:In the above code, blogcomponent uses the useStatehook to manage the selected article state. When an article is clicked on the article-list, it displays the full content of the selected article using the articlecomponent with the showFullContentprop set to true. This component orchestrates the interaction between the article-list and articlecomponents.Now, to consume the feature micro frontend, let’s create a React Native app that will act as the application runtime. Bit allows you to use app components that make their separate running environment, which can be deployed as a single application. Using this you can create your React-Native app inside Bit and you can make use of all your components.To Create a new React Native app component using Bit run the below command:bit create react-native apps/blog-consumerThis is what your folder structure looks like :Your application is inside the app namespace:Now you need to define your app component. Go to the app/blog-consumer.tsx file in the 'apps/blog-consumer directory and define the component :Make sure to replace your correct scope with @shehan97105/blog-rn-mfsIn the above BlogConsumerfunction, you can see the usage of the Blogcomponent. It will consume the newly created Blog component. You can customize this and add more micro frontends.Now save all the unsaved files first. To enable your application to be loaded as a standalone runtime, you’ll have to set it up as an aspect. To do this, execute the below command:Next, run the bit run blog-consumerto launch your application. You will be able to see the below output as your commands running.As you can you are using the micro front-end feature you built. You can have a look at the code here.Now it’s time to share your work with your team members.First, you need to version your code. To version that let’s tag the code. Navigate to the root directory of your Bit workspace. In your terminal, run the bit tag command to tag.The bit will analyze your components and prompt you to provide a version and a description for the tag. This versioning helps you keep track of changes and manage updates more effectively.After tagging, your components are ready to be shared and consumed by others.Now, the next step is exporting them. To export your tagged components, follow these steps:Still, in the root directory of your Bit workspace, run the bit export command to export your components.This will generate a package containing your tagged components along with their dependencies. This package can be installed and used in other projects.By exporting your components, you’re creating a bridge for others to use your microfrontend components effortlessly. This encourages code reusability, reduces duplication of effort, and fosters a modular development approach.Congratulations! You’ve successfully build a micro frontend-based React Native application using Bit. You can expand on this example by adding additional features and components to build a fully functional application with multiple micro frontends.Microfrontends have emerged as a solution in application development. They help address challenges related to scalability, maintainability and flexibility.By breaking down frontends into units, development teams can work simultaneously, reducing bottlenecks and improving efficiency. This article provides a step-by-step guide on creating a React Native microfrontend using Bit. It demonstrates how to implement these principles and highlights the advantages of modularization and code reuse.By utilizing tools like Bit, developers can enhance their approach to frontend architecture. This allows them to adapt to evolving industry trends and create applications that are well-equipped for the dynamic digital landscape.blog.bitsrc.ioblog.bitsrc.ioblog.bitsrc.ioblog.bitsrc.ioblog.bitsrc.ioblog.bitsrc.io----Bits and PiecesRoyalist | Software Engineer | Postgraduate student in Big Data Analytics|Ravidu PererainBits and Pieces--Robert Maier-SilldorffinBits and Pieces--12nairiharinBits and Pieces--5Ravidu PererainBits and Pieces--Tayfun KayainStackademic--1Vinod Bhat--1Roman Andrieiev--4Aleksandra Pytko-WłodarczykinCallstack Engineers--1Olayidecodes--Marcos Pereira Júnior--6HelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Building React Native Micro Frontends

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×