<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Feres Nefzi on Medium]]></title>
        <description><![CDATA[Stories by Feres Nefzi on Medium]]></description>
        <link>https://medium.com/@feresnefzi18?source=rss-c03ded66867e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NPvKnYrInKtI9dnvSUhDlg.jpeg</url>
            <title>Stories by Feres Nefzi on Medium</title>
            <link>https://medium.com/@feresnefzi18?source=rss-c03ded66867e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 05:23:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@feresnefzi18/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Navigating React State Types: Understanding and Managing Local, Global, and Server State]]></title>
            <link>https://medium.com/@feresnefzi18/navigating-react-state-types-understanding-and-managing-local-global-and-server-state-7793fc48836b?source=rss-c03ded66867e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7793fc48836b</guid>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[jotai]]></category>
            <category><![CDATA[zustand]]></category>
            <category><![CDATA[react-query]]></category>
            <dc:creator><![CDATA[Feres Nefzi]]></dc:creator>
            <pubDate>Tue, 17 Jan 2023 19:30:04 GMT</pubDate>
            <atom:updated>2023-01-17T19:30:04.888Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mLwim8jrZsSPU4yl" /><figcaption>Photo by <a href="https://unsplash.com/@lautaroandreani?utm_source=medium&amp;utm_medium=referral">Lautaro Andreani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, we will take a deep dive into the world of s<strong>tate management in React</strong>. As a developer, you know that state is an essential aspect of building web applications, but managing state can be a challenging task, <strong>especially in large and complex applications</strong>.</p><p><strong>State management</strong> is all about storing and updating data, and it can have a significant impact on the performance and maintainability of an application. Properly managing state is crucial for creating web applications that are <strong>responsive, scalable, and easy to maintain</strong>. In this article, we will explore the different types of state that can exist in a React application. We will also examine the <strong>best practices and techniques</strong> for managing each type of state, and provide you with a better understanding of how to effectively manage and update state in your React applications.</p><p>So, if you’re a developer looking to <strong>improve your state management skills</strong> and create <strong>better, more performant web applications</strong>, this article is for you. We’ll provide you with the knowledge and tools you need to <strong>navigate the different types of state in React and manage them effectively.</strong></p><h3>What is a state ? And why is it important to know how to manage it ?</h3><p>The concept of state in React refers to <strong>the data or variables</strong> that determine a component’s behavior and render information to the user. In React, <strong>state is an object that holds the information</strong> that a component needs to render and interact with the user. The state can include things like user data, form inputs, and the current page or view. It is used to <strong>store and update</strong> data in a component.</p><p>Effective state management can help to<strong> optimize the performance</strong> of an application by reducing the number of <strong>unnecessary re-renders</strong>. When state is updated in a component, React will re-render that component and all its child components. By managing state efficiently and minimizing unnecessary updates, <strong>the performance of the application can be improved</strong>.</p><p>It also helps to <strong>improve the maintainability of an application</strong>. When state is managed effectively, it becomes <strong>easier to track and identify changes in the application</strong>, making it <strong>easier to debug and test the application</strong>. This can also make it easier to add new features and make changes to the application without introducing bugs or breaking existing functionality.</p><p>Additionally, <strong>proper state management</strong> makes it <strong>easier</strong> for multiple developers <strong>to work on the same application</strong>, as state is more predictable and easy to understand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*kXwhh-38BLbXVkBeD9ngkw.png" /><figcaption>React State Types</figcaption></figure><h3>Local State</h3><p>Local state is <strong>managed by the component itself</strong>, and it is used to store information that is specific to that component. For example, a component that displays a form might have local state to keep track of the form’s input values, a component that fetches data from an API might have local state to store the data, or a component that controls a modal might have local state to store whether the modal is open or closed.</p><p>In React, <strong>local state</strong> is managed using the useState hook, which allows a component to<strong> maintain</strong> a piece of state and <strong>update</strong> it over time.</p><p>Check out the useState <a href="https://beta.reactjs.org/reference/react/useState">documentation for more information.</a></p><p>It’s important to note that local state<strong> should be used only when the data it stores is specific to the component</strong>, and it should not be used when data is shared across multiple components. In that case, <strong>global state</strong> should be used instead.</p><h3>Global State</h3><p>Global state in React refers to data or variables that are accessible and shared across multiple components in an application.</p><p>There are generally <strong>two main approaches</strong> to managing global state in React: <strong>the store approach</strong> and the <strong>atomic approach</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*mbvdeom8Kj1I_y31CvDcLw.png" /><figcaption>Global State Approaches</figcaption></figure><h4><strong>A- Store approach :</strong></h4><p>This approach uses a <strong>centralized store</strong>, to manage the application’s global state. The store is a single object that contains the entire state of the application, and components can access and update the state by dispatching actions and using the store’s state.</p><p><strong>So in this case, we can use one of these libraries to make it work :</strong></p><ol><li>Redux: is a <strong>popular</strong> library for managing global state in React applications. It uses the concept of a <strong>centralized store</strong>, in which the entire state of the application is stored in a single object, and a set of rules called reducers to <strong>update the state in response to actions</strong>. This allows for predictable and consistent state updates across the entire application. <a href="https://redux.js.org/">Checkout their official website for more information</a>.</li><li>Zustand: it&#39;s a library that is inspired by Redux, but it is <strong>more lightweight</strong>, and it <strong>has a simpler API</strong>, it&#39;s focused on providing a <strong>minimalistic and flexible</strong> way of handling the global state management. <a href="https://zustand-demo.pmnd.rs/">Checkout their official website for more information</a>.</li><li>Context API: is a built-in feature in React that allows you to <strong>share data</strong> between components without having to <strong>pass props down</strong> through multiple levels of the component tree. It uses the concept of context, which is a way to <strong>pass data through the component tree</strong> without having to explicitly pass props at every level. <a href="https://beta.reactjs.org/learn/scaling-up-with-reducer-and-context">Checkout the documentation for more information</a>.</li></ol><p>This approach is generally considered to be more suitable for <strong>larger and more complex applications</strong>, as it provides a centralized way to manage and organize the application’s state.</p><h4><strong>B- Atomic approach :</strong></h4><p>This approach uses a more <strong>granular</strong> approach to manage the application’s <strong>global state</strong>, by breaking it down into <strong>smaller, independent chunks of state,</strong> called <strong>atoms</strong>. These atoms can be <strong>shared</strong> across multiple components and <strong>updated</strong> independently of each other.</p><p><strong>In this situation, one of these libraries can be utilized to achieve the desired outcome :</strong></p><ol><li>Recoil : is a library for managing state in React applications, it is developed by Facebook. It uses a more <strong>reactive approach</strong> to state management, making it easier to handle complex state changes. Instead of using a centralized store, like Redux, Recoil creates a <strong>state tree of atoms</strong>, which are independent and can be used throughout the application. <a href="https://recoiljs.org/">Checkout their website for more information</a>.</li><li>Jotai : it takes a bottom-up approach to React state management with an <strong>atomic</strong> <strong>model</strong> inspired by Recoil. One can build state by combining atoms and renders are <strong>optimized</strong> based on atom dependency. This <strong>solves the extra re-render issue of React context</strong> and eliminates the need for the memoization technique. <a href="https://jotai.org/">Checkout their website for more information</a>.</li></ol><p>This approach is considered to be more suitable for <strong>smaller and simpler applications</strong>, as it provides a more flexible and modular way to manage the application’s state.</p><p>Additionally, it’s worth noting that each approach has its own set of trade-offs, such as <strong>the store approach can be less flexible and more complex to set up and maintain</strong>, while <strong>the atomic approach can be less organized and harder to debug.</strong></p><h3>Server State</h3><p>Server state refers to data that is <strong>stored</strong> on a server and can be accessed and updated by the client-side application. This data is typically stored in a database and can be retrieved through various methods such as <strong>REST or GraphQL APIs.</strong></p><p>To handle<strong> server state</strong> in a React application, you can use libraries like Apollo, or fetch, axios etc. to make requests to the server and retrieve data.</p><p>Additionally, you can use hooks such as useEffect and useState to handle loading, error and success states, so you can easily handle situations where the client is disconnected from the internet.</p><p>However, using <strong>TanStack React Query </strong>(more known as useQuery) makes server state management easier by providing a simple and intuitive API that allows you to declaratively fetch data and handle loading, error, and success states, and handle more complex scenarios with caching, offline support and real-time updates.</p><p>useQuery by itself <strong>doesn&#39;t handle the actual data fetching</strong>, it only provides a way to<strong> handle the loading, error, and response states</strong>, so you can use it with any library that can handle data-fetching such as <strong>Axios, Fetch, or Apollo</strong>.</p><p>Checkout the<a href="https://tanstack.com/query/latest"> TanStack React Query website</a> for more information</p><h3>Conclusion</h3><p>It’s important to consider how the client-side and server-side states are managed and how they interact with each other, to build a robust and scalable application.</p><p>To help you make an <strong>informed decision</strong> on the best way to manage your state, we’ve included this visual to guide you through picking the right way</p><figure><img alt="How to choose the best way to manage your state" src="https://cdn-images-1.medium.com/max/1024/1*w7IdmjVnl0qUMba0cdUWoQ.png" /></figure><p><strong>There are other libraries and tools that can be used to manage state in React</strong>. Each library has its own set of features and trade-offs, and the choice of which library to use depends on the specific requirements and constraints of your application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7793fc48836b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>