<?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: Alessia Bellisario</title>
    <description>The latest articles on Forem by Alessia Bellisario (@alessbell).</description>
    <link>https://forem.com/alessbell</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%2F15320%2F40a8e412-b45a-44da-b6db-901a10730272.png</url>
      <title>Forem: Alessia Bellisario</title>
      <link>https://forem.com/alessbell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alessbell"/>
    <language>en</language>
    <item>
      <title>Don’t Overreact! Introducing Apollo Client’s new @nonreactive directive and useFragment hook</title>
      <dc:creator>Alessia Bellisario</dc:creator>
      <pubDate>Thu, 15 Jun 2023 21:23:12 +0000</pubDate>
      <link>https://forem.com/apollographql/dont-overreact-introducing-apollo-clients-new-nonreactive-directive-and-usefragment-hook-1ipn</link>
      <guid>https://forem.com/apollographql/dont-overreact-introducing-apollo-clients-new-nonreactive-directive-and-usefragment-hook-1ipn</guid>
      <description>&lt;p&gt;Efficient React apps are selective in their re-rendering, using a variety of techniques (such as &lt;strong&gt;&lt;a href="https://react.dev/reference/react/memo"&gt;&lt;code&gt;memo&lt;/code&gt;&lt;/a&gt;)&lt;/strong&gt; to skip re-rendering unchanged components. With the beta release of Apollo Client 3.8, we’re excited to spotlight a couple of new techniques to help you re-render exactly the components that you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;a href="https://www.apollographql.com/docs/react/data/directives/#nonreactive"&gt;&lt;code&gt;@nonreactive&lt;/code&gt; directive&lt;/a&gt;&lt;/strong&gt;: mark GraphQL query fields and fragments that shouldn’t trigger a re-render if their cached value changes.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;a href="https://www.apollographql.com/docs/react/api/react/hooks-experimental/#usefragment"&gt;&lt;code&gt;useFragment&lt;/code&gt; hook&lt;/a&gt;&lt;/strong&gt;: create a live binding into the Apollo Client cache for a GraphQL fragment. This allows Apollo Client to broadcast specific fragment results to individual React components so child components can re-render without needing to re-render the parent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at how we can use these (both individually and in combination) to keep our app snappy!&lt;/p&gt;

&lt;h2&gt;
  
  
  (Re-)Rendering Lists
&lt;/h2&gt;

&lt;p&gt;You might have found yourself reaching for &lt;code&gt;memo&lt;/code&gt; while building an app with Apollo Client that renders a &lt;strong&gt;list&lt;/strong&gt;. Consider the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;UserFragment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gql&lt;/span&gt;&lt;span class="s2"&gt;`
  fragment UserFragment on User {
    name
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ALL_USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gql&lt;/span&gt;&lt;span class="s2"&gt;`
  query AllUsers {
    users {
      id
      ...UserFragment
    }
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;UserComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;UserList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ALL_USERS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserComponent&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;))}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ApolloClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InMemoryCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;fragments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createFragmentRegistry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gql&lt;/span&gt;&lt;span class="s2"&gt;`
      &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;UserFragment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
    `&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ApolloProvider&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserList&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ApolloProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;By registering our UserFragment with Apollo Client’s InMemoryCache via createFragmentRegistry, we can reference it by name inside our queries (as we do here with the ...UserFragment spread in ALL_USERS) without interpolating it directly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this example, the &lt;code&gt;UserList&lt;/code&gt; component fetches a list of users with Apollo Client’s &lt;code&gt;useQuery&lt;/code&gt; hook, then maps over the result to render a &lt;code&gt;UserComponent&lt;/code&gt; for each user in the list.&lt;/p&gt;

&lt;p&gt;But what happens when a user record is updated in the Apollo Client cache? Because these cached fields are watched by our &lt;code&gt;useQuery&lt;/code&gt; invocation, the hook re-runs. This re-renders &lt;code&gt;UserList&lt;/code&gt; with the latest values, which in turn recursively re-renders every child component (by default). In other words, if a single user in our list is updated, the vast majority of our &lt;code&gt;UserComponent&lt;/code&gt;s will re-render even though they haven’t changed!&lt;/p&gt;

&lt;p&gt;These unnecessary renders often go unnoticed by end users (and sometimes developers), but recursively re-running application and library code can add up to cause noticeable dips in performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  To &lt;code&gt;memo&lt;/code&gt; or not to &lt;code&gt;memo&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The React docs provide excellent guidance for this question in the deep dive section &lt;a href="https://react.dev/reference/react/memo#should-you-add-memo-everywhere"&gt;“Should you add memo everywhere?”&lt;/a&gt; Like many optimizations, &lt;code&gt;memo&lt;/code&gt; has a cost: the additional step of comparing old and new props on every render. Although that cost is often outweighed by the benefits, it’s still a cost you should consider.&lt;/p&gt;

&lt;p&gt;I encourage you to read that section in its entirety, but here are two criteria from it that can help us determine whether we should optimize with &lt;code&gt;memo&lt;/code&gt; (emphasis mine):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does our component &lt;strong&gt;re-render often&lt;/strong&gt; with &lt;strong&gt;the same exact props&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;And is its &lt;strong&gt;re-rendering logic expensive&lt;/strong&gt;, causing &lt;strong&gt;perceptible lag&lt;/strong&gt; when it re-renders?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s take a look at an example that extends the &lt;code&gt;UserList&lt;/code&gt; code above.&lt;/p&gt;

&lt;p&gt;When editing a single user and writing to Apollo Client’s &lt;code&gt;InMemoryCache&lt;/code&gt; on every keystroke, we see that the parent component re-renders, along with all of the 2,000 &lt;code&gt;UserComponent&lt;/code&gt;s in our list. Typing in the input feels sluggish 🐌&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x6BsIGep--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td25x1esg8qsdeyqwbnh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x6BsIGep--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td25x1esg8qsdeyqwbnh.gif" alt="A list of users rendered in a bare bones React app: each row renders an input with the user name pre-populated when the edit button is clicked. Typing in the input is slow and janky." width="636" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Of course, this is a contrived example: there are other ways we might write this code in a real app to work around the problem: using &lt;a href="https://www.apollographql.com/docs/react/pagination/overview/"&gt;pagination&lt;/a&gt;, debouncing cache writes so we aren’t updating the user list on every keystroke, or writing to the cache only after the user presses “Done editing”.&lt;/p&gt;

&lt;p&gt;But there are reasons we shouldn’t be satisfied with these “fixes”. First, although rendering fewer users and/or performing fewer cache writes would improve perceived performance, we’d still be re-rendering &lt;strong&gt;parent&lt;/strong&gt; and &lt;strong&gt;child&lt;/strong&gt; components unnecessarily every time a user updates. Reducing the number of renders is a good place to start, but the dynamic nature of lists—iterating over N users, where N is usually known only at runtime—can make this tricky.&lt;/p&gt;

&lt;p&gt;Maybe at first we don’t think we’re handling enough data to really need pagination, only to later notice performance has degraded when our &lt;code&gt;ALL_USERS&lt;/code&gt; query returns more users than expected (been there 🙋🏻‍♀️). Or maybe our &lt;code&gt;UserComponent&lt;/code&gt; starts out with a simple implementation that does minimal work on each render, but as our app grows it either becomes more complex or renders more complex children of its own.&lt;/p&gt;

&lt;p&gt;I hope by now I’ve convinced you this is a problem worth solving! But is &lt;code&gt;memo&lt;/code&gt; the answer?&lt;/p&gt;

&lt;p&gt;Using the criteria from the React docs, our &lt;code&gt;UserComponent&lt;/code&gt; &lt;strong&gt;is a good candidate for memoization&lt;/strong&gt;: all other users in the list are re-rendering with the same exact props ✅, and each component is simulating expensive rendering logic, causing the DOM to perceptibly lag when applying updates to 2,000+ nodes on every keystroke ✅.&lt;/p&gt;

&lt;p&gt;Sure enough, after wrapping &lt;code&gt;UserComponent&lt;/code&gt; in &lt;code&gt;memo&lt;/code&gt;, we can see the lag disappear:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FR0kGxYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no0ikc9clso58q119j94.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FR0kGxYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no0ikc9clso58q119j94.gif" alt="The same bare bones React app, but now it's wrapping each UserComponent in React.memo. The jank is gone, but we can still see that the parent component is re-rendering on every keystroke, just the unchanged children are no longer re-rendering." width="636" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun fact: even though &lt;a href="https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array"&gt;objects, arrays and functions normally need to be memoized/cached when passing them as props&lt;/a&gt; to memoized React components (so they pass React’s Object.is same value equality check when determining whether the child needs to re-render), we don’t have to memoize the user objects we’re passing into our UserComponents. This is because Apollo Client’s normalized cache has already done the work to ensure the user objects we get from the cache are referentially stable 🎉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Rendering &lt;code&gt;memo&lt;/code&gt; unnecessary
&lt;/h2&gt;

&lt;p&gt;The lag is gone, but our work isn’t done. Notice that the &lt;em&gt;parent component is still re-rendering on every keystroke&lt;/em&gt;, because our &lt;code&gt;useQuery&lt;/code&gt; call is watching for changes on all &lt;code&gt;users&lt;/code&gt;. &lt;strong&gt;We’re still re-rendering the parent, but now React is able to skip re-rendering all unchanged child components.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Re-rendering the parent has its own drawbacks: in a larger application, we’d also have to remember to wrap every other child of &lt;code&gt;UserList&lt;/code&gt; in &lt;code&gt;memo&lt;/code&gt; when needed (see the criteria above, rinse and repeat). And &lt;code&gt;memo&lt;/code&gt;‘s comparison of all of those props still comes with a cost—one we’d like to avoid altogether.&lt;/p&gt;

&lt;p&gt;Our Apollo Client team saw an opportunity here. We wondered: what if a parent component could ignore updates to certain fields on the selection set it’s watching in the cache? And what if each child component could react to updates for a single cache entity, without being prompted to re-render by its parent?&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter &lt;code&gt;@nonreactive&lt;/code&gt; and &lt;code&gt;useFragment&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Together, this new directive and hook unlock a pattern for rendering lists that selectively re-render individual list items by default—&lt;strong&gt;no &lt;code&gt;memo&lt;/code&gt; required&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can apply the &lt;code&gt;@nonreactive&lt;/code&gt; directive to fields or fragments on the parent component’s query, and you can use it with or without Apollo Client’s React bindings (for example, in queries passed to &lt;code&gt;client.watchQuery&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;In our demo app, the &lt;code&gt;AllUsers&lt;/code&gt; query becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AllUsers&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="n"&gt;users&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="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="n"&gt;UserFragment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;@nonreactive&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;blockquote&gt;
&lt;p&gt;Note: The parent component &lt;em&gt;should&lt;/em&gt; receive updates for the &lt;code&gt;id&lt;/code&gt; field (or whatever the equivalent cache key is for an entity), so we intentionally &lt;em&gt;don’t&lt;/em&gt; apply &lt;code&gt;@nonreactive&lt;/code&gt; to the &lt;code&gt;id&lt;/code&gt; field here. This ensures that the parent &lt;strong&gt;does&lt;/strong&gt; update when items are added or removed from the list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, when cache updates arrive and Apollo Client is comparing the previous result with incoming data for a selection set to determine whether to update, it can avoid comparing fields that exist on subtrees of the &lt;code&gt;@nonreactive&lt;/code&gt; fragment altogether. Less work for Apollo Client, and less work for React as a result.&lt;/p&gt;

&lt;p&gt;Instead of passing each user object into &lt;code&gt;UserComponent&lt;/code&gt; and wrapping it with &lt;code&gt;memo&lt;/code&gt;, child components can use the &lt;code&gt;id&lt;/code&gt; and fragment document to receive cache updates directly for each user in the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;UserComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useFragment&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserFragment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;__typename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any component watching for changes for a specific user via &lt;code&gt;useFragment&lt;/code&gt; will re-render on its own when data changes. It no longer needs to wait for a parent to trigger a re-render by passing it fresh data via props!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2TLygPWV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agdqm2bkb9ylgirxve5m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2TLygPWV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agdqm2bkb9ylgirxve5m.gif" alt="The same bare bones React app, but now using @nonreactive and useFragment: only the child component being edited re-renders - not the parent or any siblings - and the app is smooth and performant." width="636" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, our parent component &lt;strong&gt;no longer re-renders at all when updating users&lt;/strong&gt;—only when users are &lt;strong&gt;added&lt;/strong&gt; or &lt;strong&gt;removed&lt;/strong&gt;. This gives us predictable behavior &lt;em&gt;and&lt;/em&gt; performance, whether our list is 10, 100, or 1,000 users long! And we achieved all of this without &lt;code&gt;memo&lt;/code&gt;. (You definitely &lt;em&gt;should&lt;/em&gt; still use &lt;code&gt;memo&lt;/code&gt; as needed to solve other performance issues, per the guidance in the docs!)&lt;/p&gt;

&lt;p&gt;View the full CodeSandbox demo &lt;a href="https://codesandbox.io/s/github/alessbell/nonreactive"&gt;here&lt;/a&gt;. You can try out both &lt;code&gt;@nonreactive&lt;/code&gt; and &lt;code&gt;useFragment&lt;/code&gt; in our latest 3.8 beta via &lt;code&gt;npm i @apollo/client@beta&lt;/code&gt; and view the documentation for both &lt;a href="https://www.apollographql.com/docs/react/data/directives/#nonreactive"&gt;&lt;code&gt;@nonreactive&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://www.apollographql.com/docs/react/api/react/hooks-experimental/#usefragment"&gt;&lt;code&gt;useFragment&lt;/code&gt;&lt;/a&gt; for more information. We’d love to hear what you think!&lt;/p&gt;

</description>
      <category>react</category>
      <category>graphql</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
