<?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: Facundo Siracusa</title>
    <description>The latest articles on DEV Community by Facundo Siracusa (@faq885).</description>
    <link>https://dev.to/faq885</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%2F201135%2Fc415d4e1-cf9c-437f-a9fd-d7c365267d52.jpeg</url>
      <title>DEV Community: Facundo Siracusa</title>
      <link>https://dev.to/faq885</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/faq885"/>
    <language>en</language>
    <item>
      <title>Apollo, React Adopt and Redux</title>
      <dc:creator>Facundo Siracusa</dc:creator>
      <pubDate>Mon, 28 Oct 2019 15:44:04 +0000</pubDate>
      <link>https://dev.to/faq885/apollo-react-adopt-and-redux-1ofn</link>
      <guid>https://dev.to/faq885/apollo-react-adopt-and-redux-1ofn</guid>
      <description>&lt;p&gt;&lt;code&gt;Post originally posted in Medium:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://medium.com/@facusiracusa/how-to-use-react-adopt-and-redux-connect-f410a17d2899?sk=98053d07ca94b447bee1eb10952fa28d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The other day I was in the need to rewrite an Apollo container, render props are nice, but you can also be lost in a callbacks hell if you have several mutations and query components to mix up. Talking with a work mate, he suggested me &lt;strong&gt;react-adopt&lt;/strong&gt;, but even if he gave me an example I needed to go further and mix that with redux function connect.&lt;/p&gt;

&lt;p&gt;So since I spent time trying different approaches to do that, surfing the web without much success, and trying to understand react-adopt examples, I thought to write this article and maybe speed up others work with a concrete real life example.&lt;br&gt;
Consider this container with 2 mutations and 2 queries, the container needs to use one query or another depending on a url parameter, and also be connected to a redux store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
    const { match } = this.props;
    const isSellRequest = match.params.isSellRequest == 'true';
    return (
      &amp;lt;Mutation
        mutation={updateSellRequestStatus}
      &amp;gt;
        {(updateSellRequestStatus, { loading, ...mutation2Props }) =&amp;gt; {
          const loadingMut2 = loading;
          return (
            &amp;lt;Mutation
              mutation={createSell}
              refetchQueries={[
                {
                  query: getSellsQuery,
                  variables: {
                    page: 0,
                    limit: SELLS_PAGE_LIMIT,
                    filter: ''
                  }
                }
              ]}
              awaitRefetchQueries
            &amp;gt;
              {(createSell, { loading, ...mutationProps }) =&amp;gt; {
                const loadingMut = loading;
                const Comp = ({ data, loadingQ, other }) =&amp;gt; (
                  &amp;lt;WrappedComponent
                    createSell={createSell}
                    updateSellRequestStatus=    {updateSellRequestStatus}
                    request={get(data, 'node', null) || null}
                    {...mutationProps}
                    {...this.props}
                    {...other}
                    loading={loadingQ || loadingMut || loadingMut2}
                    isSellRequest={isSellRequest}
                  /&amp;gt;
                );
                if (isSellRequest) {
                  return (
                    &amp;lt;Query
                      query={sellRequestQuery}
                      variables={{
                        id: match &amp;amp;&amp;amp; match.params.id
                      }}
                    &amp;gt;
                      {({ data, loading, ...other }) =&amp;gt; {
                        return (
                          &amp;lt;Comp 
                             data={data}
                             other={other}
                             loadingQ={loading} 
                          /&amp;gt;;
                        )
                      }}
                    &amp;lt;/Query&amp;gt;
                  );
                } else {
                  return (
                    &amp;lt;Query
                      query={quoteRequestQuery}
                      variables={{
                        id: match &amp;amp;&amp;amp; match.params.id
                      }}
                    &amp;gt;
                      {({ data, loading, ...other }) =&amp;gt; {
                        return (
                          &amp;lt;Comp 
                            data={data} 
                            other={other} 
                            loadingQ={loading} 
                          /&amp;gt;;
                        )
                      }}
                    &amp;lt;/Query&amp;gt;
                  );
                }
              }}
            &amp;lt;/Mutation&amp;gt;
          );
        }}
      &amp;lt;/Mutation&amp;gt;
    );
export default connect(mapStateToProps)(CreateSellFromReqContainer);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yes, I know, impossible to understand and debug it!! So, let's talk first about &lt;em&gt;react-adopt&lt;/em&gt;.&lt;br&gt;
Taking the description from its page, React Adopt is a simple method that composes multiple render prop components, combining each prop result from your mapper. For example you can use it like this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { adopt } from 'react-adopt'

import { User, Cart, ShippingRate } from 'my-containers'

const Composed = adopt({
  cart: &amp;lt;Cart /&amp;gt;,
  user: &amp;lt;User /&amp;gt;,
  shippingRates: ({ user, cart, render }) =&amp;gt; (
    &amp;lt;ShippingRate zipcode={user.zipcode} items={cart.items}&amp;gt;
      {render}
    &amp;lt;/ShippingRate&amp;gt;
  )
})

&amp;lt;Composed&amp;gt;
  {({ cart, user, shippingRates }) =&amp;gt; /* ... */ }
&amp;lt;/Composed&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To see more examples, you can check its own github page &lt;a href="https://github.com/pedronauck/react-adopt"&gt;https://github.com/pedronauck/react-adopt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Ok, so first we will rewrite each part of the container individually to be used by adopt, let's see how we achieve this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const UpdateRequestMutation = ({ render }) =&amp;gt; (
  &amp;lt;Mutation mutation={updateSellRequestStatus}&amp;gt;
    {(updateSellRequestStatus, { loading, ...mutationProps }) =&amp;gt;
      render({ updateSellRequestStatus, loadingUpdate: loading, ...mutationProps })
    }
  &amp;lt;/Mutation&amp;gt;
);

const CreateSellMutation = ({ render }) =&amp;gt; (
  &amp;lt;Mutation
    mutation={createSell}
    refetchQueries={[
      {
        query: getSellsQuery,
        variables: {
          page: 0,
          limit: SELLS_PAGE_LIMIT,
          filter: ''
        }
      }
    ]}
    awaitRefetchQueries
  &amp;gt;
    {(createSell, { loading, ...mutation2Props }) =&amp;gt;
      render({ createSell, loadingCreate: loading, ...mutation2Props })
    }
  &amp;lt;/Mutation&amp;gt;
);

const SellRequestQuery = ({ render, match }) =&amp;gt; {
  const isSellRequest = match.params.isSellRequest == 'true';
  return (
    &amp;lt;Query
      query={sellRequestQuery}
      variables={{
        id: match &amp;amp;&amp;amp; match.params.id
      }}
      skip={!isSellRequest}
    &amp;gt;
      {({ data, loading }) =&amp;gt; render({ sellRequest: get(data, 'node', null) || null, loading })}
    &amp;lt;/Query&amp;gt;
  );
};

SellRequestQuery.propTypes = {
  match: object,
  render: func
};

const QuoteRequestQuery = ({ render, match }) =&amp;gt; {
  const isSellRequest = match.params.isSellRequest == 'true';
  return (
    &amp;lt;Query
      query={quoteRequestQuery}
      variables={{
        id: match &amp;amp;&amp;amp; match.params.id
      }}
      skip={isSellRequest}
    &amp;gt;
      {({ data, loading }) =&amp;gt;
        render({ quoteRequest: get(data, 'node', null) || null, loadingQR: loading })
      }
    &amp;lt;/Query&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can see, we extracted our two queries and mutations separately, you can choose what to return from each component inside its return method. For example the &lt;em&gt;SellRequestQuery&lt;/em&gt; will return these props, &lt;em&gt;sellRequest&lt;/em&gt; and &lt;em&gt;loading&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{({ data, loading }) =&amp;gt; render({ 
  sellRequest: get(data, 'node', null) || null, 
  loading 
})}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;that will be accessible in our composed component. Putting it all together with react-adopt results in a component like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Composed = adopt({
   CreateSellMutation,
   UpdateRequestMutation,
   QuoteRequestQuery,
   SellRequestQuery
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we have to mix all the properties and return our composed component in our container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CreateFromSellRequestContainer = props =&amp;gt; {
  const { match } = props;
  const isSellRequest = match.params.isSellRequest == 'true';
  return (
    &amp;lt;Composed match={match}&amp;gt;
      {({
        CreateSellMutation: { createSell, loadingCreate },
        SellRequestQuery: { loading, sellRequest },
        QuoteRequestQuery: { loadingQR, quoteRequest },
        UpdateRequestMutation: { updateSellRequestStatus, loadingUpdate }
      }) =&amp;gt; {
        return (
          &amp;lt;WrappedComponent
            isSellRequest={isSellRequest}
            loading={loading || loadingCreate || loadingUpdate || loadingQR}
            createSell={createSell}
            updateSellRequestStatus={updateSellRequestStatus}
            request={sellRequest || quoteRequest}
            {...props}
          /&amp;gt;
        );
      }}
    &amp;lt;/Composed&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can see, the composed component receives four root properties, one for each item used in the adopt function that englobes the properties returned by each component in its return statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CreateSellMutation: { createSell, loadingCreate },
SellRequestQuery: { loading, sellRequest },
QuoteRequestQuery: { loadingQR, quoteRequest },
UpdateRequestMutation: { updateSellRequestStatus, loadingUpdate }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So basically, in our container we are taking all these properties and we are re formatting and passing them to the View component as we need it. &lt;/p&gt;

&lt;p&gt;The last step is to return it and connect the container with the redux store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default connect(mapStateToProps(CreateFromSellRequestContainer);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I know this can be improved with the new hooks fashion, but it is not the scope of this article, it is just intended to help others to understand how react-adopt works and how it can be used to improve the readability and extensibility of a container created using apollo render props components.&lt;/p&gt;

</description>
      <category>react</category>
      <category>adopt</category>
      <category>redux</category>
      <category>connect</category>
    </item>
  </channel>
</rss>
