<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhishek Tomar</title>
    <description>The latest articles on DEV Community by Abhishek Tomar (@imabhishektomar).</description>
    <link>https://dev.to/imabhishektomar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F270490%2F5ac5287a-6621-4901-910c-6bd461f97c8f.jpeg</url>
      <title>DEV Community: Abhishek Tomar</title>
      <link>https://dev.to/imabhishektomar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imabhishektomar"/>
    <language>en</language>
    <item>
      <title>Maximizing Code Reusability with Yarn Workspaces (Monorepo)</title>
      <dc:creator>Abhishek Tomar</dc:creator>
      <pubDate>Mon, 18 Mar 2024 11:45:41 +0000</pubDate>
      <link>https://dev.to/imabhishektomar/maximizing-code-reusability-with-yarn-workspaces-monorepo-103g</link>
      <guid>https://dev.to/imabhishektomar/maximizing-code-reusability-with-yarn-workspaces-monorepo-103g</guid>
      <description>&lt;p&gt;In the fast-paced world of software development, maximizing efficiency and code reusability is crucial. Enter Yarn Workspaces, a powerful tool for creating monorepos that streamline development workflows and promote code sharing across projects. In this post, we'll delve into the benefits and practical applications of Yarn Workspaces, exploring how they can revolutionize your development process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Yarn Workspaces
&lt;/h3&gt;

&lt;p&gt;Yarn Workspaces allow developers to manage multiple projects within a single repository. By consolidating related projects into one cohesive workspace, developers can more easily share code, dependencies, and resources across projects. This approach not only simplifies project management but also enhances code modularity and reusability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Monorepo with Yarn Workspaces
&lt;/h3&gt;

&lt;p&gt;Let's walk through the process of setting up a monorepo using Yarn Workspaces. Assume we're developing two ReactJS websites that share common components and functionalities. By leveraging Yarn Workspaces, we can organize these projects within a unified workspace, facilitating seamless code sharing and collaboration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initializing the Workspace&lt;/strong&gt;: Start by creating a new directory for your monorepo and initializing it with Yarn.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;yarn-workspace-example &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;yarn-workspace-example
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Defining Workspaces:&lt;/strong&gt; Update the package.json file to specify the directories containing your projects.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"private"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"yarn-workspace-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MIT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"workspaces"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"apps/*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"libs/*"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Creating Projects:&lt;/strong&gt; Develop your projects within the designated directories (apps for applications, libs for shared libraries).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;apps
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn create react-app app_one
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn create react-app app_two
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sharing Code:&lt;/strong&gt; Develop common libraries or components within the libs directory, ensuring seamless integration with your projects.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;libs
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn create react-library common
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Utilizing Workspaces:&lt;/strong&gt; Leverage Yarn Workspaces commands to manage dependencies and scripts across projects.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn workspace app_one start
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn workspace app_two start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits of Yarn Workspaces
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Code Reusability:&lt;/strong&gt; Share common libraries and components across projects, reducing redundancy and promoting consistency.&lt;/p&gt;

&lt;p&gt;Simplified Dependency Management: Manage dependencies at the workspace level, avoiding conflicts and ensuring uniformity.&lt;br&gt;
Enhanced Collaboration: Facilitate collaboration among team members by providing a centralized environment for code sharing and development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Yarn Workspaces offer a powerful solution for managing monorepos and maximizing code reusability. By consolidating projects within a unified workspace, developers can streamline development workflows, foster collaboration, and build more efficient and maintainable software applications. Embrace the potential of Yarn Workspaces to elevate your development process and unlock new possibilities in code sharing and collaboration. Happy coding!&lt;/p&gt;

</description>
      <category>yarn</category>
      <category>react</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Mastering Git Committing: Best Practices and Guidelines</title>
      <dc:creator>Abhishek Tomar</dc:creator>
      <pubDate>Wed, 28 Feb 2024 19:41:55 +0000</pubDate>
      <link>https://dev.to/imabhishektomar/mastering-git-committing-best-practices-and-guidelines-1e1</link>
      <guid>https://dev.to/imabhishektomar/mastering-git-committing-best-practices-and-guidelines-1e1</guid>
      <description>&lt;p&gt;In the realm of version control systems, Git stands out as one of the most powerful and widely adopted tools. Yet, its effectiveness greatly depends on how well it's utilized, particularly when it comes to committing changes. Git commits are not just snapshots of code; they are the building blocks of collaboration and code evolution. Here, we delve into some best practices and guidelines for mastering Git committing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Best Practices for Commit Messages: Starting with Clear Prefixes. e.g. FIX, RELEASE, FEATURE, BUG, REFACTOR, TEST, BUILD etc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Commit with Purpose
&lt;/h3&gt;

&lt;p&gt;A pivotal aspect of Git committing is ensuring that each commit encapsulates a coherent set of changes. Whether it's fixing a bug or implementing a feature, commit granularity enhances clarity and facilitates efficient collaboration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Embrace Frequency
&lt;/h3&gt;

&lt;p&gt;Committing frequently is akin to creating checkpoints in your development journey. By breaking down changes into smaller, manageable commits, developers promote seamless integration and mitigate the risk of conflicts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shun Half-Baked Commits
&lt;/h3&gt;

&lt;p&gt;Avoid the temptation to commit incomplete work. Each commit should represent a logically complete unit of work, ensuring that changes are robust and actionable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Validate Before You Commit
&lt;/h3&gt;

&lt;p&gt;Prior to committing, rigorously test your code to ensure its integrity and functionality. While local commits offer leeway for experimentation, shared commits demand a higher standard of quality assurance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Craft Compelling Messages
&lt;/h3&gt;

&lt;p&gt;The art of writing commit messages cannot be overstated. A well-crafted commit message serves as a narrative, guiding future developers through the evolution of code changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Harness Branching Power
&lt;/h3&gt;

&lt;p&gt;Branching is Git's cornerstone feature, empowering developers to explore new ideas and isolate changes without disrupting the main codebase. Embrace branches as a versatile tool in your development arsenal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Establish Workflow Consensus
&lt;/h3&gt;

&lt;p&gt;Choose a Git workflow that aligns with your project's needs and team dynamics. Consensus on a common workflow fosters cohesion and enhances productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Examples and Guidelines
&lt;/h2&gt;

&lt;p&gt;Let's explore some exemplary commit messages that embody these principles:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: Sort summery based commit&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Commit Hash: 3114a97ba188895daff4a3d337b2c73855d4632d
Author: [Abhishek]
Date: Mon Jun 11 17:16:10 2012 +0100
FIX: Changes has been done with the new c++ runtime module update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2: Description in multiple points&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Commit Hash: ae878fc8b9761d099a4145617e4a48cbeb390623
Author: [Abhishek]
Date: Fri Jun 1 01:44:02 2012 +0000

FEATURE: Done with processing module

- Minimized duplicated code for create.
- Modified wait_for_destroy to occur on shutdown instead of undefine.
- Enabled destruction of an instance while leaving the domain intact.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3: Large details&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Commit Hash: 31336b35b4604f70150d0073d77dbf63b9bf7598
Author: [Abhishek]
Date: Wed Jun 6 22:45:25 2012 -0400

RELEASE: Key features of this enhancement include:

- Identification and storage of guest arches in the permitted_instances_types list within the cpu_info dictionary of the host.
- Implementation of the arch filter to compare the instance arch against permitted instances types, filtering out invalid hosts.
- Addition of ARM as a valid arch to the filter, ensuring compatibility with diverse architectures.

Notably, the ArchFilter remains deactivated by default, offering flexibility in its utilization.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Best Practices for Commit Messages: Starting with Clear Prefixes
&lt;/h3&gt;

&lt;p&gt;When crafting Git commit messages, always beginning with a clear prefix enhances readability and organization. Let's explore some common prefixes and their significance:&lt;/p&gt;

&lt;p&gt;Prefixes for Commit Messages&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FIX: Addressing Issues&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting a commit message with "FIX" indicates that it resolves a specific issue or bug within the codebase. These commits focus on improving the stability and functionality of the software by addressing existing problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RELEASE: Version Updates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Commits prefixed with "RELEASE" signify significant milestones in the project, such as the release of a new version or major feature set. These commits often include updates to version numbers, changelogs, and release notes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FEATURE: Introducing New Functionality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using "FEATURE" as a prefix denotes the addition of new features or functionalities to the software. These commits expand the capabilities of the application and contribute to its ongoing development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BUG: Fixing Bugs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"BUG" prefixed commits indicate the resolution of specific bugs or issues. These commits focus on debugging and ensuring the smooth operation of the software by addressing reported problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REFACTOR: Code Refactoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting a commit message with "REFACTOR" signifies restructuring or optimizing existing code without changing its external behavior. These commits aim to improve code quality, readability, and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DOC: Documentation Updates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prefixing commits with "DOC" indicates changes or additions to documentation, including README files, code comments, or developer guides. These commits enhance the clarity and accessibility of project documentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TEST: Adding or Modifying Tests&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using "TEST" as a prefix involves additions or modifications to test suites, ensuring comprehensive test coverage and validating the correctness of the software.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STYLE: Code Style Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Commits prefixed with "STYLE" involve updates to code formatting, styling, or conventions. These commits focus on improving the consistency and aesthetics of the codebase.&lt;/p&gt;

&lt;p&gt;By starting commit messages with these clear prefixes, developers can streamline collaboration, improve code quality, and maintain a well-organized project history.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In essence, Git committing is more than just a technical task; it's a strategic endeavor that shapes the evolution of code. By adhering to best practices and guidelines, developers can optimize collaboration, ensure code quality, and drive continuous improvement in software development.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>Maximising React Development Efficiency with Monorepo Architecture</title>
      <dc:creator>Abhishek Tomar</dc:creator>
      <pubDate>Sat, 06 Jan 2024 13:05:24 +0000</pubDate>
      <link>https://dev.to/imabhishektomar/maximising-react-development-efficiency-with-monorepo-architecture-8b8</link>
      <guid>https://dev.to/imabhishektomar/maximising-react-development-efficiency-with-monorepo-architecture-8b8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the ever-evolving landscape of web development, efficient project organization is crucial for maintaining scalable and maintainable codebases. One powerful approach gaining popularity is the monorepo architecture, especially in the context of React applications. In this blog post, we'll explore the benefits and implementation of a monorepo setup for React projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Monorepo:
&lt;/h3&gt;

&lt;p&gt;A monorepo, short for monolithic repository, is a version control system where multiple projects coexist within a single repository. In the context of React development, these projects can include various applications, libraries, and shared components. This consolidated structure simplifies code sharing, dependency management, and overall project maintenance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Monorepo for React:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Code Sharing:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One of the primary advantages is the ease of sharing code between different projects. Shared components, utilities, and modules can be centralized, reducing redundancy and promoting consistency across applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Simplified Dependency Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Monorepos leverage tools like Yarn Workspaces or Lerna to manage dependencies efficiently. This ensures that all projects use the same versions of shared dependencies, avoiding conflicts and streamlining updates.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Atomic Changes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Atomic commits become more manageable as changes across multiple projects can be committed together. This helps maintain a consistent state of the codebase, making it easier to track and roll back changes when necessary.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Unified Development Environment:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developers can work on different parts of the project without the hassle of switching between repositories. A unified development environment enhances collaboration and accelerates the development process.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cross-Project Refactoring:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large-scale refactoring becomes less daunting when all related projects reside in a single repository. Developers can make sweeping changes across the codebase with confidence, knowing that they can update dependent projects simultaneously.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Setting Up a Monorepo for React:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Choose a Tool:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select a monorepo management tool like Yarn Workspaces or Lerna based on your project's requirements. Both tools simplify the process of managing dependencies and scripts across multiple packages.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Organize Your Projects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Divide your React projects into packages within the monorepo. Common structures include having separate folders for applications, shared components, and libraries. This organization enhances clarity and maintainability.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Shared Dependencies:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clearly define and manage shared dependencies. Ensure that all projects use the same version of React, React Router, and other key dependencies to prevent compatibility issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Build Scripts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adapt build scripts to handle the compilation and bundling of individual packages. Tools like Yarn Workspaces automatically manage this, while Lerna provides additional configuration options for customization.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Implementing a monorepo architecture for React projects offers numerous benefits, from improved code sharing to streamlined dependency management. By centralizing your codebase, you can enhance collaboration, simplify maintenance, and accelerate development. Embrace the monorepo paradigm to unlock new levels of efficiency and scalability in your React projects.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>monorepo</category>
      <category>react</category>
      <category>yarn</category>
    </item>
    <item>
      <title>Create Mobx Store In React Native using Hooks &amp; Combining multiple stores</title>
      <dc:creator>Abhishek Tomar</dc:creator>
      <pubDate>Tue, 23 Feb 2021 14:17:52 +0000</pubDate>
      <link>https://dev.to/imabhishektomar/create-mobx-store-in-react-native-using-hooks-combining-multiple-stores-4hkm</link>
      <guid>https://dev.to/imabhishektomar/create-mobx-store-in-react-native-using-hooks-combining-multiple-stores-4hkm</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Create your stores&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Todo {
  id: number;
  text: string;
}
class TodoStore {
  root: RootStore;
  listTodo: Todo[] = [];
  constructor(root: RootStore) {
    makeAutoObservable(this);
    this.root = root;
  }
  setTodo(todo: Todo) {
    this.listTodo.push(todo);
  }

  get getTodoAll() {
    return this.listTodo;
  }

  getTodoId(id: number) {
    return this.listTodo.filter((f) =&amp;gt; f.id == id);
  }
}

class OtherStore {
  root: RootStore;
  totalTodoCount: number = 0;
  constructor(root: RootStore) {
    makeAutoObservable(this);
    this.root = root;
  }

  init() {
    // safe to access other stores
    const allData = this.root.todo.getTodoAll;
    this.totalTodoCount = allData.length;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create root store &amp;amp; combine all stores in root&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class RootStore {
  todo: TodoStore;
  other: OtherStore;
  constructor() {
    this.todo = new TodoStore(this);
    this.other = new OtherStore(this);

    this.other.init();
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create Provider&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let store: RootStore;
const StoreContext = createContext&amp;lt;RootStore | undefined&amp;gt;(undefined);
export default function RootStoreProvider({children}: {children: ReactNode}) {
  const root = store ?? new RootStore();
  return &amp;lt;StoreContext.Provider value={root}&amp;gt;{children}&amp;lt;/StoreContext.Provider&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create hooks for accessing the store&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function useRootStore() {
  const context = useContext(StoreContext);
  if (context === undefined) {
    throw new Error('useRootStore must be used within RootStoreProvider');
  }
  return context;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create UI screen&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AppScreen1 = observer(() =&amp;gt; {
  const store = useRootStore();
  const AddNew = () =&amp;gt; {
    const ID = Math.floor(1000 + Math.random() * 9000);
    store.todo.setTodo({id: ID, text: `Todo Item ${ID}`});
  };
  return (
    &amp;lt;View style={{flex: 1}}&amp;gt;
      {store.todo.getTodoAll.map((item) =&amp;gt; {
        return (
          &amp;lt;Text style={{fontWeight: 'bold'}}&amp;gt;
            {item.id.toString()}/{item.text}
          &amp;lt;/Text&amp;gt;
        );
      })}
      &amp;lt;Button title="Add New Todo" onPress={AddNew} /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Create app entry&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const AppEntry = () =&amp;gt; {
  return (
    &amp;lt;RootStoreProvider&amp;gt;
      &amp;lt;AppScreen1 /&amp;gt;
    &amp;lt;/RootStoreProvider&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/ivandotv/mobx-root-store-pattern-with-react-hooks-318d"&gt;https://dev.to/ivandotv/mobx-root-store-pattern-with-react-hooks-318d&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mobx.js.org/README.html"&gt;https://mobx.js.org/README.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/leighhalliday/mobx2021/blob/main/src/store.tsx"&gt;https://github.com/leighhalliday/mobx2021/blob/main/src/store.tsx&lt;/a&gt;&lt;br&gt;
&lt;a href="https://unsplash.com/photos/vc3iVL_znJ8"&gt;https://unsplash.com/photos/vc3iVL_znJ8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>mobx</category>
      <category>typescript</category>
      <category>hooks</category>
    </item>
  </channel>
</rss>
