<?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: Alexander Mandrov</title>
    <description>The latest articles on DEV Community by Alexander Mandrov (@unsleeping).</description>
    <link>https://dev.to/unsleeping</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%2F3607299%2F557eb3b3-4a73-41c0-8c17-a639cdacf196.png</url>
      <title>DEV Community: Alexander Mandrov</title>
      <link>https://dev.to/unsleeping</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/unsleeping"/>
    <language>en</language>
    <item>
      <title>Localizing React re-renders: the magic of compositional boundaries</title>
      <dc:creator>Alexander Mandrov</dc:creator>
      <pubDate>Wed, 12 Nov 2025 08:50:07 +0000</pubDate>
      <link>https://dev.to/unsleeping/localizing-react-re-renders-the-magic-of-compositional-boundaries-39og</link>
      <guid>https://dev.to/unsleeping/localizing-react-re-renders-the-magic-of-compositional-boundaries-39og</guid>
      <description>&lt;p&gt;Ever felt your UI lag when nothing “should” be slow?&lt;/p&gt;

&lt;p&gt;That’s when localizing re-renders works wonders. It’s not about micro-optimizations, it’s about owning your composition structure.&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Split context responsibilities&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;const SetItemContext = createContext&amp;lt;Dispatch&amp;lt;number&amp;gt;&amp;gt;(() =&amp;gt; {});
const ItemContext = createContext&amp;lt;number&amp;gt;(0);

export const Provider = ({ children }: { children: React.ReactNode }) =&amp;gt; {
  const [item, setItem] = useState(1);
  return (
    &amp;lt;SetItemContext.Provider value={setItem}&amp;gt;
      &amp;lt;ItemContext.Provider value={item}&amp;gt;{children}&amp;lt;/ItemContext.Provider&amp;gt;
    &amp;lt;/SetItemContext.Provider&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consumers tied only to the setter won’t re-render on value change.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Optimize TanStack Query subscriptions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Avoid destructuring query results before returning them from your custom hooks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ❌ Causes all consumers to re-render
export const useUser = () =&amp;gt; {
  const { data, error, isLoading } = useQuery(...);
  return { data, error, isLoading };
};

// ✅ Gives per-field subscription
export const useUser = () =&amp;gt; {
  const useQueryResult = useQuery(...);
  return useQueryResult
}


// consumer 1 - loading indicator renderer
const { isLoading } = useUser()

// consumer 2 - data renderer
const { data } = useUser()

// consumer 3 - error tracker
const { error } = useUser()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now data, error, and isLoading consumers re-render independently.&lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;Render prop = localized reactivity&lt;/strong&gt;&lt;br&gt;
Move your stateful logic closer to where it’s used via render props or children. React will reuse the evaluated element if props are unchanged. No need for memo overkill.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Component = () =&amp;gt; {
    const filledHeight = useCalculatedHeight(); // Imagine this changes frequently

    return (
        &amp;lt;div sx={{ height: `calc(100% - ${filledHeight}px` }}&amp;gt;
            {/* 
                This NestedComponent will re-render EVERY TIME 'filledHeight' changes,
                even if 'nestedProps' haven't changed.
                React sees that the parent Component re-rendered, and by default,
                it will re-render all its children.
            */}
            &amp;lt;NestedComponent {...nestedProps} /&amp;gt; 
        &amp;lt;/div&amp;gt;
    );
};


// This component now *receives* its child, rather than creating it
const PerformantComponent = ({ nestedComponent }) =&amp;gt; {
    const filledHeight = useCalculatedHeight(); // This still changes frequently

    return (
        &amp;lt;div sx={{ height: `calc(100% - ${filledHeight}px` }}&amp;gt;
            {/* 
                Now, 'nestedComponent' is a PROP.
                If the PARENT of PerformantComponent passes the *same* 'nestedComponent' 
                prop reference, React will NOT re-render it when 'filledHeight' changes.
                It reuses the already evaluated element.
            */}
            {nestedComponent} 
        &amp;lt;/div&amp;gt;
    );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with composition before you reach for memoization.&lt;/p&gt;

&lt;p&gt;Good composition is performance.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webperf</category>
      <category>performance</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
