<?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>Forem: shirisha95</title>
    <description>The latest articles on Forem by shirisha95 (@sanikommushirisha).</description>
    <link>https://forem.com/sanikommushirisha</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%2F925231%2F5e295d24-e65d-47f0-b0c2-a34a06b5c4d0.png</url>
      <title>Forem: shirisha95</title>
      <link>https://forem.com/sanikommushirisha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sanikommushirisha"/>
    <language>en</language>
    <item>
      <title>7 Techniques to Optimize React Applications Performance</title>
      <dc:creator>shirisha95</dc:creator>
      <pubDate>Sat, 10 Dec 2022 11:08:31 +0000</pubDate>
      <link>https://forem.com/sanikommushirisha/efficient-components-techniques-to-optimise-react-applications-performance-3l9l</link>
      <guid>https://forem.com/sanikommushirisha/efficient-components-techniques-to-optimise-react-applications-performance-3l9l</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Optimizing the performance of a React application is an important aspect of maintaining a high-quality user experience. It requires deeper understanding of React framework, various phases of the components lifecycle and profiling tools to measure bottlenecks and analyze performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiah36lz0wrd8dt8khvhq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiah36lz0wrd8dt8khvhq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are several techniques you can use to improve the performance of your React application.Below are some tips you can follow to optimize React performance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the &lt;code&gt;shouldComponentUpdate&lt;/code&gt; lifecycle method to avoid unnecessary re-renders. This method allows you to control when a component is updated by determining if the new props or state are different from the previous ones.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;PureComponent&lt;/code&gt; class instead of the Component class for components that do not require complex logic. Pure components implement the shouldComponentUpdate method with a shallow comparison of the props and state, so they only re-render if the props or state have changed.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;React.memo&lt;/code&gt; higher-order component to optimize functional components. This is similar to PureComponent, but it can be used with functional components instead of class-based components.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;React.Suspense&lt;/code&gt; components to lazy-load components that are not immediately needed. This can improve initial loading time by only loading the necessary components on demand.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt; hooks to optimize functions and computed values within components. These hooks allow you to memoize values and functions so that they are not re-created unnecessarily.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;React.Fragment&lt;/code&gt; component to avoid unnecessary wrapper elements in your components. Wrapper elements, such as divs, can add extra nodes to the DOM, which can affect performance. React.Fragment allows you to return multiple elements from a component without adding extra nodes to the DOM.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Immutable Data Structures&lt;/code&gt; as they are more efficient to compare than mutable data structures. They have zero side-effects, simpler to create, test, and use, and can prevent unnecessary re-renders&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;🎉 Bonus Tip:&lt;/strong&gt;&lt;br&gt;
Use the profiler built into React to identify performance bottlenecks and optimize the rendering of your components. The profiler can help you understand which components are rendering frequently and why, allowing you to make changes to improve their performance.&lt;/p&gt;

&lt;p&gt;By following these tips, you can improve the performance of your React application and provide a better user experience for your users. It is also important to regularly monitor and profile your application to identify areas for optimization and ensure that it continues to perform well.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding React Context</title>
      <dc:creator>shirisha95</dc:creator>
      <pubDate>Tue, 13 Sep 2022 17:54:42 +0000</pubDate>
      <link>https://forem.com/sanikommushirisha/understanding-react-context-2ebg</link>
      <guid>https://forem.com/sanikommushirisha/understanding-react-context-2ebg</guid>
      <description>&lt;p&gt;If you think about it, the top-down data flow is what gave react superpowers. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have more control over your data. No more early JS days where you sweep through heaps &amp;amp; loads of JS files and figure out what mutated your state. &lt;/li&gt;
&lt;li&gt;It’s easier to debug, as you know what is coming from where&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But what if we are building a deeply nested app?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      App Contents
      &amp;lt;Toolbar theme={store.theme} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

const Toolbar = ({ theme }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Button theme={theme} label="Button 1" /&amp;gt;
      &amp;lt;Button theme={theme} label="Button 2" /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

const Button = ({ theme, label }) =&amp;gt; {
  return (
    &amp;lt;button style={{ backgroundColor: theme === "dark" ? "black" : "white" }}&amp;gt;
      {label}
    &amp;lt;/button&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;This is called prop-drilling, and this gets even worse if you have more layers of components between the data source and user. How to solve this? React Context to the rescue!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solving prop drilling by using the Context API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Context API lets you broadcast your state/data to multiple components by wrapping them with a context provider. Pass your state as value attribute to the contextProvider, the children components can then tap into this provider using a context consumer or the useContext Hook.&lt;/p&gt;

&lt;p&gt;Step 1: Create Context&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ThemeContext.jsx
import React from "react";
const ThemeContext = React.createContext();
export default ThemeContext;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Context Provider: &lt;br&gt;
Now we can wrap all the context users with the Context Provider, and pass the value that we want to 'broadcast'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  return (
    &amp;lt;ThemeContext.Provider value={{ theme: store.theme }}&amp;gt;
      App Contents
      &amp;lt;Toolbar /&amp;gt;
    &amp;lt;/ThemeContext.Provider&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, how do we access the theme from its descendants Toolbar, Button?&lt;/p&gt;

&lt;p&gt;Step 3: Context Consumer: useContext&lt;br&gt;
To access the context, we use useContext hook from any descendant of Component &lt;code&gt;App&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { ThemeContext } from "./ThemeContext";

const Button = ({ theme, label }) =&amp;gt; {
  const { theme } = React.useContext(ThemeContext);
  return (
    &amp;lt;button style={{ backgroundColor: theme === "dark" ? "black" : "white" }}&amp;gt;
      {label}
    &amp;lt;/button&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, Our rendered output remains the same, but the code underneath is a bit leaner and cleaner.&lt;/p&gt;

&lt;p&gt;That's all! I hope that I helped clarify why you need context and how to implement it. Feel free to post any questions, comments or any suggestions.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
