<?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: Yariv Shamash</title>
    <description>The latest articles on DEV Community by Yariv Shamash (@yarivshamash).</description>
    <link>https://dev.to/yarivshamash</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%2F571358%2F8bdc88a2-c12c-415f-b9dd-32feed28a6f3.jpeg</url>
      <title>DEV Community: Yariv Shamash</title>
      <link>https://dev.to/yarivshamash</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yarivshamash"/>
    <language>en</language>
    <item>
      <title>Managing State in React</title>
      <dc:creator>Yariv Shamash</dc:creator>
      <pubDate>Sun, 21 Jul 2024 14:09:07 +0000</pubDate>
      <link>https://dev.to/yarivshamash/managing-state-in-react-21ee</link>
      <guid>https://dev.to/yarivshamash/managing-state-in-react-21ee</guid>
      <description>&lt;p&gt;As React developers we have several options when it comes to managing state in our applications. &lt;br&gt;
Each component we create can hold state and share it with its children. But what happens when we want to share state across sibling components? How should it be done?&lt;br&gt;
This article focuses on three popular approaches: Redux, atomic state management, and React Context. We'll also compare these with the "pro bono" context provided by TanStack Router.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Redux
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://redux.js.org/" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; is one of the most popular libraries but not necessarily one of the easiest ones to use. Its a predictable state container for JavaScript apps, widely used in the React ecosystem. It has powerful developer tools that help understand and debug the apps state and also a large ecosystem of middleware and extensions.&lt;br&gt;
On the other hand using Redux requires a steep learning curve of the relations between a reducer, an action and a dispatcher and also quite a lot of boilerplate code.&lt;br&gt;
Redux is actually notorious for that boilerplate code, which is something that I don't recommend overlooking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Atomic State Management
&lt;/h3&gt;

&lt;p&gt;Atomic state management solutions, like &lt;a href="https://recoiljs.org/" rel="noopener noreferrer"&gt;Recoil&lt;/a&gt; or &lt;a href="https://jotai.org/" rel="noopener noreferrer"&gt;Jotai&lt;/a&gt;, break down state into small, reusable units called atoms.&lt;br&gt;
It's reducing unnecessary re-renders by allowing components to subscribe only to the specific pieces of state they need. When a state update occurs, only the components that depend on that particular atom will re-render, rather than triggering a re-render of the entire component tree or large sections of it.&lt;br&gt;
It's so easy and comfortable to user, I remember the first time I used it I thought to myself "Wow! that's the way it should be done!" basically if you understood react's &lt;code&gt;useState&lt;/code&gt; hook you'll understand how to use these libraries.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Context
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://react.dev/learn/scaling-up-with-reducer-and-context" rel="noopener noreferrer"&gt;React Context&lt;/a&gt; is a built-in feature that allows passing data through the component tree without props drilling.&lt;br&gt;
It requires setting up the specific context and a context-provider (a special react component).&lt;br&gt;
You can use react's built in reducer to manage the state of the context and add all sorts of methods to handle the context as you wish.&lt;br&gt;
After doing it each and every component that sits under the context provider in the component tree will have access to everything you exposed using the context provider.&lt;br&gt;
It's a convenient way to separate concerns but it has a few drawbacks.&lt;br&gt;
It's a bit cumbersome to use and requires creating a provider. Also, once your app becomes more complex and has many different concerns and therefore providers, you'll need to maintain all of that and make sure the context pyramid is built well and maintained.&lt;/p&gt;

&lt;h3&gt;
  
  
  TanStack Router's "Pro Bono" Context
&lt;/h3&gt;

&lt;p&gt;TanStack Router (formerly React Router) provides a &lt;a href="https://tanstack.com/router/latest/docs/framework/react/guide/router-context" rel="noopener noreferrer"&gt;context&lt;/a&gt; as part of its routing solution. It's also has somewhat of a learning curve and not as intuitive as I expected one I used it first. But it's there and if your app is small and simple, you should consider using it because it enables us to run some context related actions before loading a page or specifying a loader function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary and conclusions
&lt;/h2&gt;

&lt;p&gt;For small to medium-sized applications with simple state requirements, React Context or is often sufficient and easy to implement, especially if you don't want to add another package to your project. &lt;br&gt;
If you're already using tanstack-router consider using it's built in context as it is easier to use and provides some lifecycle methods. Saying that, it shouldn't be relied upon as a complete state management solution.&lt;/p&gt;

&lt;p&gt;Atomic state management solutions like Recoil or Jotai are excellent for applications that need fine-grained control and optimized performance, especially when working with React's concurrent features (disclaimer, it's my favorite solution).&lt;/p&gt;

&lt;p&gt;For larger applications or those with complex state logic, Redux remains a solid choice due to its ecosystem and powerful dev tools.&lt;/p&gt;

&lt;p&gt;Ultimately, the best choice depends on your specific project requirements, team expertise, and scalability needs. It's often beneficial to start with the simplest solution (like React Context) and gradually adopt more complex tools as your application grows and demands increase.&lt;/p&gt;

&lt;p&gt;I'll be happy to help and answer your questions here. Alternatively you can reach me on &lt;a href="https://www.linkedin.com/in/yariv-shamash-162bb1a7/" rel="noopener noreferrer"&gt;my LinkedIn account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding :)&lt;/p&gt;

&lt;p&gt;P.S. here's a bullet point summery for your convenience:&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros and cons Summery
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Redux
&lt;/h3&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predictable state updates&lt;/li&gt;
&lt;li&gt;Powerful developer tools for debugging&lt;/li&gt;
&lt;li&gt;Large ecosystem of middleware and extensions&lt;/li&gt;
&lt;li&gt;Well-suited for complex state logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Steep learning curve&lt;/li&gt;
&lt;li&gt;Requires more boilerplate code&lt;/li&gt;
&lt;li&gt;Can be overkill for smaller applications&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Atomic State Management
&lt;/h3&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine-grained updates, reducing unnecessary re-renders.&lt;/li&gt;
&lt;li&gt;Easy to use and understand, almost no learning curve.&lt;/li&gt;
&lt;li&gt;Scales well with application size&lt;/li&gt;
&lt;li&gt;Works great with React's concurrent features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Newer approach, potentially less community support.&lt;/li&gt;
&lt;li&gt;May require a mental shift for developers used to global state&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  React Context
&lt;/h3&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native to React, no additional libraries required&lt;/li&gt;
&lt;li&gt;Simple to set up and use&lt;/li&gt;
&lt;li&gt;Great for passing global data (e.g., themes, user info)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance concerns with large-scale updates&lt;/li&gt;
&lt;li&gt;Can lead to deeply nested providers&lt;/li&gt;
&lt;li&gt;Not optimized for frequent updates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  TanStack Router's "Pro Bono" Context
&lt;/h3&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Seamlessly integrated with routing&lt;/li&gt;
&lt;li&gt;No additional setup required if already using TanStack Router&lt;/li&gt;
&lt;li&gt;Useful for route-specific data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited to routing-related state&lt;/li&gt;
&lt;li&gt;Not a complete state management solution&lt;/li&gt;
&lt;li&gt;Potential for misuse if overloaded with unrelated data&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>architecture</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Next.js and &lt;svg /&gt;</title>
      <dc:creator>Yariv Shamash</dc:creator>
      <pubDate>Sun, 20 Mar 2022 09:34:26 +0000</pubDate>
      <link>https://dev.to/yarivshamash/nextjs-and-587b</link>
      <guid>https://dev.to/yarivshamash/nextjs-and-587b</guid>
      <description>&lt;p&gt;A quick share of my experiance with Next.js and &lt;code&gt;.svg&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;I needed to display a bunch of &lt;code&gt;.svg&lt;/code&gt; files on a Next.js app.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;.svg&lt;/code&gt; files with Next.js super cool &lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt; component is &lt;a href="https://nextjs.org/docs/api-reference/next/image#dangerously-allow-svg" rel="noopener noreferrer"&gt;not recommended&lt;/a&gt; since it can create unexpected behavior and lead to vulnerabilities if a security policy header is not defined.&lt;/p&gt;

&lt;p&gt;So I needed to incorporate a loader. &lt;br&gt;
The quickest way I found was using &lt;code&gt;.babelrc&lt;/code&gt; file and &lt;code&gt;babel-plugin-inline-react-svg&lt;/code&gt;.&lt;br&gt;
It worked! well kind of.. 😕 Some graphics were not displayed and some did, while others were cut off unexpectedly.&lt;/p&gt;

&lt;p&gt;I dug a bit more and found &lt;a href="https://stackoverflow.com/a/55180310/11528989" rel="noopener noreferrer"&gt;this answer&lt;/a&gt; by &lt;a href="https://stackoverflow.com/users/6539317/felixmosh" rel="noopener noreferrer"&gt;@felixmosh&lt;/a&gt; on stackoverflow using &lt;code&gt;next.config.js&lt;/code&gt; file.&lt;/p&gt;
&lt;h4&gt;
  
  
  TL;DR:
&lt;/h4&gt;

&lt;p&gt;~ Step One:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save-dev @svgr/webpack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;~ Step Two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// next.config.js

module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      issuer: {
        test: /\.(js|ts)x?$/,
       // for webpack 5 use
       // { and: [/\.(js|ts)x?$/] }
      },

      use: ['@svgr/webpack'],
    });

    return config;
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you used the above solution make sure to give the question and the answer a &lt;a href="https://stackoverflow.com/a/55180310/11528989" rel="noopener noreferrer"&gt;vote up on stackoverflow&lt;/a&gt; 🙂&lt;/p&gt;




&lt;p&gt;Side note, if you’re using typescript in your project and feel bad that the next.config file is a .js file don’t because next does not plan “to transpile next.config.js.” As you can see in &lt;a href="https://github.com/vercel/next.js/issues/5318" rel="noopener noreferrer"&gt;this issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope it helped, thanks for reading!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
