<?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: Rich11</title>
    <description>The latest articles on DEV Community by Rich11 (@richardmen11).</description>
    <link>https://dev.to/richardmen11</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%2F634225%2Fc9f44e38-3b21-4954-9f8b-2aa692311afd.png</url>
      <title>DEV Community: Rich11</title>
      <link>https://dev.to/richardmen11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/richardmen11"/>
    <language>en</language>
    <item>
      <title>How to Start Vision Pro Development (Today)</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Mon, 11 Mar 2024 06:27:39 +0000</pubDate>
      <link>https://dev.to/richardmen11/how-to-start-vision-pro-development-today-5cba</link>
      <guid>https://dev.to/richardmen11/how-to-start-vision-pro-development-today-5cba</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When Apple unfurled the announcement of its latest innovation, the Vision Pro, my enthusiasm surged. Why? Because this was not just another addition to their tech portfolio, it was a whole new platform. A limitless canvas where we, the pioneers of code, could truly let our creativity run wild.&lt;/p&gt;

&lt;p&gt;When the news hit, my immediate sentiment was awe. But, concurrently, a question lurked: '&lt;em&gt;This is revolutionary, but my arena is web development. Can I craft projects for this kind of device?&lt;/em&gt;' The challenge was daunting but tantalizing. Here was an opportunity to stride beyond my comfort zone, to master a new set of skills, despite having minimal experience with building native apps.&lt;/p&gt;

&lt;p&gt;Months later, I find myself on the brink of releasing my second app for the Vision Pro. The journey has taught me how to construct simple but effective apps for this platform. But this narrative isn't about me. This is your ticket to join the Vision Pro realm, an invitation to dive into a 3D world, to create, innovate and manifest your ideas into tangible applications.&lt;/p&gt;

&lt;p&gt;Are you ready to leap into the future of tech?&lt;br&gt;
Join me as we explore, learn, and build for Apple's Vision Pro.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do you need to know?
&lt;/h2&gt;

&lt;p&gt;Swift is the foundation for developing applications for Apple platforms, including Vision Pro. Let's break down the process into accessible steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Swift
&lt;/h2&gt;

&lt;p&gt;First thing is learning how to code. But that's a another topic. I assume you know how to program - at least a bit. Because that makes the journey a lot easier. Otherwise I would encourage you to start with coding. &lt;/p&gt;

&lt;p&gt;Swift is Apple's own programming language and you need it to build any kind of native Apple product. This is especially true for vision pro. As far as I know right now it is the easiest way to do it. &lt;/p&gt;

&lt;p&gt;But how can you learn it now? There are many great resources out there. There is even a &lt;a href="https://developer.apple.com/learn/"&gt;book&lt;/a&gt; about the language from Apple themself. I like to learn, just the basics and don't try to overcomplicate things. Because we want to get into building apps as soon as possible. &lt;/p&gt;

&lt;p&gt;Learn some things - then start building. Later dive into the details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Platform specifics
&lt;/h2&gt;

&lt;p&gt;After you have learned about swift - you have to learn about the platform. For vision pro it is the same as for iOS apps. You can build interfaces with SwiftUI. I think it helps if you build a small iOS project first. Because you probably know some parts of other platforms or even use the devices yourself. &lt;/p&gt;

&lt;p&gt;Just get familiar with building layouts in SwiftUI. How an iOS project can be structured and what are the patterns that can be used in native development. There are some great resources which I will share later. These will help you to build your first iOS app. &lt;/p&gt;

&lt;p&gt;Now comes the fun part. Getting familiar with the vision Pro platform. If you have a device. Use it. I think it is an advantage. If you do not. It is no problem. You can just try the simulator. Try the already installed apps. Install some of the example projects from Apple - just learn how this device and the simulator can be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Windows
&lt;/h2&gt;

&lt;p&gt;Your vision Pro app will start with the first window it finds. Which will be presented to the user. You can move windows through the space. They are mostly 2D. You can imagine them as big Ipad apps floating through your space. I love that. &lt;/p&gt;

&lt;p&gt;You can create more of these windows. But you can not set their position. So play around with creating windows. Open and close them on the click of a button. &lt;/p&gt;

&lt;h2&gt;
  
  
  Immersive Space
&lt;/h2&gt;

&lt;p&gt;This is the new part. Building immersive experiences. These also can be opened programmatically. If they are opened - you can basically use the whole space and place 3D objects in it.&lt;/p&gt;

&lt;p&gt;Apple added a way to make that really easy. If you have a 3D object - you can just use the 3DModel file and place it there. Do that. Play with the 3D content. It gives us as developers so many more possibilities. &lt;/p&gt;

&lt;p&gt;From there on you can use a lot of the stuff which is already available. Apple pushed AR technologies already over the years. They have built a lot of things for that. You can use them but not always. That's the beauty of being an early adopter. &lt;/p&gt;

&lt;p&gt;But there are also some resources from Apple on how you can build great immersive experiences.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the ressources?
&lt;/h2&gt;

&lt;p&gt;You now know what is needed to build vision pro apps. But where can you learn more about it? There are many things you can do. Here are some resources I found helpful. &lt;/p&gt;

&lt;h2&gt;
  
  
  Apple WWDC content
&lt;/h2&gt;

&lt;p&gt;The Apple Vision Pro was released at the WWDC 2023 - therefore Apple made a lot of videos for developers to understand how to build for the new device. &lt;/p&gt;

&lt;p&gt;You can find all of the videos here: &lt;a href="https://developer.apple.com/visionos/learn/"&gt;https://developer.apple.com/visionos/learn/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of the videos are awesome. Even the ones that are not really development specific are very interesting and I promise you will learn a lot.&lt;/p&gt;

&lt;p&gt;Really helpful also is the section about &lt;a href="https://developer.apple.com/news/?id=tqkgshu3"&gt;developer news&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;There you can find a really helpful Q&amp;amp;A which gives you a lot of insights. Furthermore you can look at the developer stories and how other developers have built apps for the vision pro which I found really motivating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Projects
&lt;/h2&gt;

&lt;p&gt;Apple themself published some &lt;a href="https://developer.apple.com/documentation/visionos/"&gt;example projects&lt;/a&gt;. Which are very nice. But do not get frustrated. The code is very complex. I even heard some experienced developers tell that. For me they helped to understand - how projects for vision pro can be built and what are some things that can be done. &lt;/p&gt;

&lt;p&gt;But do not worry if you do not understand everything yet. It will get better. &lt;/p&gt;

&lt;h2&gt;
  
  
  Courses
&lt;/h2&gt;

&lt;p&gt;There are not many courses out there yet. I mean I think it does not make too much sense for a solo dedicated apple vision pro course. Why? Because in general it is not that different from developing other apps for the apple ecosystem. &lt;/p&gt;

&lt;p&gt;But there are some great courses for iOS Development. There is one a lot of people recommended to me. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/ios-13-app-development-bootcamp"&gt;iOS app development bootcamp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This course covers a lot. I did some parts of it. The good thing is that there is also a part about AR Kit which will help you to understand how to build immersive experiences. But this is designed for iOS. It can not be translated completely to visionOS.&lt;/p&gt;

&lt;p&gt;Then there is a course I would love to highlight. The CS193p from Stanford. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://cs193p.sites.stanford.edu/2023"&gt;Stanford CS193p&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This course covers great topics and you will even learn how to build your fist app. The best thing is: it is completely free. I think if you want to dip your feet into iOS Development - you should think about starting here. The newest one is currently the lectures from spring 2023.&lt;/p&gt;

&lt;p&gt;If you want a small course specifically for Vision Pro. Then I can recommend this one. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=KTFh-6LyFEI&amp;amp;list=PLzoXsQeVGa05gAK0LVLQhZJEjFFKSdup0"&gt;Build a vision pro app from scratch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is made by a youtuber - and helped me a lot to understand some things. It even builds a cool little project while learning. So if you feel ready to build your first project. Go there. &lt;/p&gt;

&lt;p&gt;The last recommendation for courses here would be the full day workshop from Hacking with Swift. I joined it live. It was amazing. It should be still available if you become a subscribing member on their site. &lt;/p&gt;

&lt;p&gt;A lot of different topics were covered and again a small app was built. This is a fun thing for maybe a weekend to level up your Vision Pro skills. Furthermore there where questions asked by the community - which likely maybe will be your questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Books
&lt;/h2&gt;

&lt;p&gt;Currently there are no books available. Or as far as I dont know of good ones. But again - building for vision pro is not that different from building for other apple platforms. &lt;/p&gt;

&lt;p&gt;So one book that helped me a lot was one from &lt;a href="https://www.kodeco.com/books/real-world-ios-by-tutorials"&gt;Kodeco&lt;/a&gt;. It covers real world tactics to build Apps. I think that helps a lot if you want to make a real app at some point. Which is not only for fun but instead will also go live in the app store.&lt;/p&gt;

&lt;p&gt;It covers a lot of topics which other books won’t. It even shows how to do testing and how to publish your app to the app store. &lt;/p&gt;

&lt;h2&gt;
  
  
  ChatGPT
&lt;/h2&gt;

&lt;p&gt;I want to leave some words about this tool here. It helped me a lot. But yes it has some flaws. So mostly ChatGPT does not know about Vision Pro - why? Because as far as I know most of the models are based on knowledge from a time where the vision pro did not exist. So how should the model know? &lt;/p&gt;

&lt;p&gt;But as mentioned before - building for apple vision pro is not groundbreaking different. So ChatGPT can help. But do not trust it blindly. Try to see it as a partner you can learn with. It can give you some hints, directions and ideas. But it does not know everything. &lt;/p&gt;

&lt;p&gt;I had some moments where I got stuck. Then it helped. It helped to generate ideas. Often the code was wrong. Or even things were invented that were completely wrong. But sometimes it could be a good idea to use this tool. &lt;/p&gt;

&lt;p&gt;And maybe one day we will get a nice coding partner from Apple. Let's see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;Start learning for apple vision pro today. It is fun. Try a new platform which will evolve over the years. Maybe you can bring completely new worlds to live through your newly gained knowledge. Wouldn’t that be cool? I think so. &lt;/p&gt;

&lt;p&gt;Looking forward to hearing your tipps, tricks and resources to learn Vision Pro Development. &lt;/p&gt;

</description>
      <category>swift</category>
      <category>visionpro</category>
      <category>apple</category>
      <category>ios</category>
    </item>
    <item>
      <title>Gatsby and Contentful - How to use a headless CMS</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Sat, 26 Jun 2021 06:10:18 +0000</pubDate>
      <link>https://dev.to/richardmen11/gatsby-and-contentful-how-to-use-a-headless-cms-2692</link>
      <guid>https://dev.to/richardmen11/gatsby-and-contentful-how-to-use-a-headless-cms-2692</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2y5vdc2t838odcof91gw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2y5vdc2t838odcof91gw.png" alt="Alt Text" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Contentful?
&lt;/h2&gt;

&lt;p&gt;Contentful is a CMS - which means Content Management System. These kinds of systems allow people&lt;br&gt;
to manage content. Maybe the best known CMS is WordPress. In contrast to WordPress Contentful is headless.&lt;br&gt;
What does this now mean? It means that it only provides the data which is presented by the CMS nothing else. A "normal" CMS would&lt;br&gt;
also provide for example a visual part. Why is this good? Because it allows us as developers to use the&lt;br&gt;
data from the headless CMS everywhere we want to. In our apps, websites and so on. The page could look completely different and the&lt;br&gt;
data stays the same.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use Contentful with Gatsby?
&lt;/h2&gt;

&lt;p&gt;As always in Gatsby we just have to use a plugin. The plugin is called &lt;a href="https://www.gatsbyjs.com/plugins/gatsby-source-contentful/"&gt;gatsby-source-contentful&lt;/a&gt;.&lt;br&gt;
This offers an easy and simple way to get the data from Contentful. Before you can start using the data, you have to set up a new account if you don't already have one.&lt;br&gt;
Just sign up &lt;a href="https://www.contentful.com/"&gt;here&lt;/a&gt; for free. The basics are free and unless you do not need roles I think you are fine with the free plan.&lt;br&gt;
Then you have to create your first space. You can image the space like a project you are making the content for.&lt;/p&gt;

&lt;p&gt;Afterwards, you just could get your space ID and your accessToken. You can create them under settings &amp;gt; API Keys and then&lt;br&gt;
generate a new one. First steps done. 🥳&lt;/p&gt;
&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;Next is the setup of your Gatsby page. Just generate a new Gatsby project or use an existing one you would like to&lt;br&gt;
add data from Contentful to. Then add the configuration to your gatsby-config file.&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="c1"&gt;// In your gatsby-config.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&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="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`gatsby-source-contentful`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;spaceId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`your_space_id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Learn about environment variables: https://gatsby.dev/env-vars&lt;/span&gt;
        &lt;span class="na"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CONTENTFUL_ACCESS_TOKEN&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="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please remember to use environment variables. That is important because if somebody could get your accessToken&lt;br&gt;
he could get all your content. Better be safe. 😉&lt;/p&gt;
&lt;h3&gt;
  
  
  First Content-Type
&lt;/h3&gt;

&lt;p&gt;Now, as we are ready for take off, we would add our first content type to Contentful. But what does ContentType mean?&lt;br&gt;
A ContentType is basically the layout of the content, for example a blog post could be one. Each post has a title, text and maybe an image.&lt;br&gt;
Together they are a content type. So let's add our first one.&lt;/p&gt;

&lt;p&gt;Therefore, you need to go to ContentModel and click on add content type. Then a new window appears which wants you to give the content type a name&lt;br&gt;
and an autogenerated identifier. You also could provide a description which is useful for your content editor because they might not know&lt;br&gt;
what a specific ContentType is used for.&lt;/p&gt;

&lt;p&gt;After you finished this step. You are ready to add your first field. Click on add field and choose a type which fits your needs the most. For example, you could choose text.&lt;br&gt;
Then you have to configure the field even further. Give it a name and change the fieldId if the auto generated not suits you. Afterwards, if you click on create and configure,&lt;br&gt;
you have the chance to sharpen the configuration even more. This could make sense in different cases, but should not be a part of this article.&lt;/p&gt;

&lt;p&gt;Then add all fields you need in your ContentType and publish the changes. Now we have added the first ContentType, but we are missing content. A ContentType is useless&lt;br&gt;
without content. So let's add content. Go to the content tab and click right on the button add content. The button name could differ depending on the ContentType pre&lt;br&gt;
selected. But if you have more than one ContentType you have the choice of which ContentType you want to add.&lt;/p&gt;

&lt;p&gt;Choose the ContentType and add the content in the predefined fields. It should be like you described it before in the ContentType. Afterwards, do not forget to&lt;br&gt;
save the content and publish it.&lt;/p&gt;

&lt;p&gt;Now we are ready to use our first content pieces. 🚀&lt;/p&gt;

&lt;p&gt;Start the Gatsby server with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gatsby develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a few seconds the server has build, and we now could check if the data is available for us. To do this, visit: &lt;a href="http://localhost:8000/%5C_%5C_%5C_graphql"&gt;http://localhost:8000/\_\_\_graphql&lt;/a&gt;&lt;br&gt;
The GraphiQL playground is really useful to see all of your data. Search for a data type called something with Contentful. The name should be something like allContentfulDataType where&lt;br&gt;
dataType is the name of your ContentType you newly created on Contentful. If everything worked, you are now able to use the data from contentful in your page query.&lt;/p&gt;

&lt;p&gt;That was easy, right?&lt;/p&gt;
&lt;h3&gt;
  
  
  Generate Pages
&lt;/h3&gt;

&lt;p&gt;A really useful feature is that you are able to generate new pages from Gatsby. This is nothing special for Contentful but works perfect together with it.&lt;br&gt;
For example, you could create a page from every blog post ContentType. So you never need to worry about adding a new page when adding a new post.&lt;/p&gt;

&lt;p&gt;How does this work? You need to open your gatsby-node file.&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="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createPages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;actions&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;actions&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/templates/template.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`
          {
            &amp;lt;YOUR_GRAPHQL_CALL&amp;gt;
          }
        `&lt;/span&gt;
      &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errors&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;content&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;

        &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;edges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;edge&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;slug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;edge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;
          &lt;span class="nf"&gt;createPage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;path&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="nx"&gt;slug&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="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="nx"&gt;slug&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="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="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a lot at once. Let me explain it. First, you get the createPages function from the actions, which we will use later.&lt;br&gt;
Then you have to return a promise. Why? Because we have to call the api from Contentful and wait for the data, and we want to create pages which&lt;br&gt;
also takes some time.&lt;/p&gt;

&lt;p&gt;Then inside the promise, we take a file we created before. This file will be the template for the new created pages. Then we make our graphql query to get the data we need.&lt;br&gt;
After the data comes back, we work with the result. We first check for an error and if everything is fine, we call the createPage function on every entries of the array&lt;br&gt;
we got from the server. The function gets a path with has to be different for every iteration, otherwise the pages would be overwritten. Then a component which should be&lt;br&gt;
our template. Lastly, it needs a context which will be available as props for the page.&lt;/p&gt;

&lt;p&gt;That's it. I think this is awesome because you do not have to write similar pages by your own, and you are able to add content via a CMS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the benefits?
&lt;/h2&gt;

&lt;p&gt;But what is all this for. Why do you need this? We as developers might not need this. Because for us, it is easier to add text in our editor. For example&lt;br&gt;
my personal blog is generated from Markdown. But if a non-technical person wants to add content to a page, Contentful is awesome.&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://www.techlabs.org"&gt;TechLabs&lt;/a&gt; we also build the page with contentful. We searched for an easy way to edit the pages of our locations. We wanted a way to give each&lt;br&gt;
location the chance to edit their subpage. Contentful makes it easy to give them an interface to add the data and also publish new content by themselves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The combination of Gatsby and Contentful could be useful for any page. It's easy to set up and gives the developer huge flexibility. As my own&lt;br&gt;
experience I only could say that I really enjoy working with Contentful. Furthermore, you could configure apps and a lot more things to even&lt;br&gt;
optimize your time with Contentful. But you have to try if by yourself. Contentful is free to set up and allows you to test most of its functionality&lt;br&gt;
for free.&lt;/p&gt;

</description>
      <category>react</category>
      <category>gatsby</category>
      <category>contentful</category>
      <category>headless</category>
    </item>
    <item>
      <title>4 Reasons for GatsbyJS and when not to use it.</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Sun, 06 Jun 2021 16:54:30 +0000</pubDate>
      <link>https://dev.to/richardmen11/4-reasons-for-gatsbyjs-and-when-not-to-use-it-3olk</link>
      <guid>https://dev.to/richardmen11/4-reasons-for-gatsbyjs-and-when-not-to-use-it-3olk</guid>
      <description>&lt;p&gt;Gatsby is a great tool to build websites and more. But often people ask me when to use it and what Gatsby is most suited for in my&lt;br&gt;
opinion. That's a good question. And the answer as is always: It depends. But that's not all. Let's have a deeper look&lt;br&gt;
on this question. I think it comes down to four points:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Performance
&lt;/h2&gt;

&lt;p&gt;Gatsby is fast. That's true. I mean, its easy-to-reach crazy good score with Gatsby. This website has a score of 91 in performance and I did not do anything&lt;br&gt;
to achieve a better score. It comes right out of the box. In comparison, a WordPress blog only reaches 63. I tested it on the blog &lt;a href="https://www.whaitbutwhy.com"&gt;whaitbutwhy&lt;/a&gt; (definitely check it out). And the performance on a site gets even worse when you use a page builder and a ton of&lt;br&gt;
different plugins or even WooCommerce. And if you want to have a fast and reliable site - you also have to build it with custom code.&lt;/p&gt;

&lt;p&gt;Ok, but what does these numbers mean. It means that you lose some people because of your site speed and also miss some opportunities on the search optimizations. Both&lt;br&gt;
are very important for an online business. I think we all had this moment where we visited a website which loads long. I think it is annoying. Or think about an online store&lt;br&gt;
you want to shop in. If every time you visit another product the site takes a long time, that would be a bad experience.&lt;/p&gt;

&lt;p&gt;In numbers that means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;40% of people abandon a website that takes more than 3  seconds to load. 🤯&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks to &lt;a href="https://neilpatel.com/blog/loading-time/"&gt;Neil Patel&lt;/a&gt; for a really in deeps article about the bottom line of a slow page.&lt;/p&gt;

&lt;p&gt;I think this number shows really drastically how important a fast page is for your business. How much money you would lose with a slow website. Gatsby helps you to avoid&lt;br&gt;
this and gives you a lot more speed than most others tools to build a site. But the benefits you get out of the box could even be optimized by using the right plugins and&lt;br&gt;
optimization of your gatsby-config file. But that's an entirely different topic.&lt;/p&gt;

&lt;p&gt;So number one is clear. If you need a really fast site &lt;b&gt;use Gatsby&lt;/b&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Flexibility
&lt;/h2&gt;

&lt;p&gt;Next up is Flexibility. This is also a good and important part. Building a page with Gatsby gives you flexibility. Which ensures to react to potentially changes in the future. Think about it&lt;br&gt;
this way. You have built a page with Gatsby. Then your page grows, and you need a CMS because there are people how need to work on the content. Than you easily could add a CMS to Gatsby. If you are&lt;br&gt;
lucky your page still grows. You think about selling merch in your own shop. That also easy to add with Gatsby, no worries. And you want to have a small part on the page where the data comes from&lt;br&gt;
airtable. That's also possible.&lt;/p&gt;

&lt;p&gt;I know that this also would be possible with something like WordPress but if you cannot code it yourself there you would have to search for a plugin and pray that this plugin suits all your needs.&lt;br&gt;
And yes, If you want to do all these things in Gatsby you also need to code, but you do not need to learn PHP and the ins and outs of WordPress. If you know react, it is enough. And also Gatsby gives you more&lt;br&gt;
flexibility on the tooling. I mean, if you want to use StyledComponents instead of CSS. You can! You like TypeScript more than plain JavaScript. Go and do it. There are no limits.&lt;/p&gt;

&lt;p&gt;I really love how flexible Gatsby is and this helps me as a developer to build different sites for different needs. And I do not have to rebuild everything if the business changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Design
&lt;/h2&gt;

&lt;p&gt;Flexibility is great. But Flexibility in Design could be a major thing. If your company has a CI which needs to be used, most page builder are not able to deal with it. With Gatsby, you could build the pixel perfect&lt;br&gt;
page.&lt;/p&gt;

&lt;p&gt;Yes, every time you have a page with a special design, you need to code it. In my opinion not every time. Because tools like Webflow are pretty good, for people with a bit of programming knowledge&lt;br&gt;
and some design skills. But this only counts for a page with not that much functionality. If you want more - you need something like Gatsby.&lt;/p&gt;

&lt;p&gt;And maybe your company has already some prebuild components which could be used in a project. Then it is easy for you to build a page with Gatsby.&lt;/p&gt;

&lt;p&gt;I think for me personally it's easier to build a design with CSS than learning a new page builder and being frustrated afterwards why some things do not work with it. But in the end&lt;br&gt;
it should be your personal preference.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Ecosystem
&lt;/h2&gt;

&lt;p&gt;The last thing to keep in mind that when building with Gatsby is the Ecosystem. Why is this important? First, if you need help, you have several places you could go. And Second there are so&lt;br&gt;
many plugins and recipes you could use to improve your page.&lt;/p&gt;

&lt;p&gt;In comparison, if you use a not that popular tool than you might end up stuck on a problem or searching for plugins and help. I would always go for a tool which either has a great customer&lt;br&gt;
support or a good Ecosystem.&lt;/p&gt;

&lt;p&gt;I can totally recommend the Gatsby Discord and docs. There are some information and helpful tools you could find an answer for your problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to use Gatsby
&lt;/h2&gt;

&lt;p&gt;As you might recognize, I like Gatsby. But I think not all the time Gatsby is the right choice. I would love to say it always makes sense, but this would be not honest.&lt;/p&gt;

&lt;p&gt;For me, it there a some points which should be considered before thinking about building with Gatsby:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can not code and do not know anybody who can code.&lt;/li&gt;
&lt;li&gt;You are superb at coding with WordPress.&lt;/li&gt;
&lt;li&gt;You only want a minimal page.&lt;/li&gt;
&lt;li&gt;You want a really graphical way to edit your site.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me give a bit more detail to all these points.&lt;/p&gt;

&lt;p&gt;If you can not code it will be hard and yes I totally would recommend learning to code, but this takes some time. If you don't want to do it, and you don't know someone who can code or if it is to&lt;br&gt;
expensive. Then you have to find another way. Building a site with Gatsby and do not know how to code is impossible in my opinion.&lt;/p&gt;

&lt;p&gt;The next should also be clear. I mean, if you have your tools and your already experienced, and you know they will fit for the job. It is fine and a good idea to work with what you know.&lt;br&gt;
If you want to try something new. I can totally recommend it Gatsby, but I think you would be faster with the tools you know.&lt;/p&gt;

&lt;p&gt;For the next point, it's also not that useful to use Gatsby. If you just won't a small page. Just use HTML and CSS. Its fast and easy. I mean, if your page is only like a small online business card.&lt;br&gt;
Why bother with a tool like Gatsby. I think then you could do it for fun but not because it is useful.&lt;/p&gt;

&lt;p&gt;The last part I would like to mention is in case you love to design your page like in a design tool. Then Gatsby is not right for you. If you need it shiny and beautiful, you should take something like&lt;br&gt;
Webflow and wix.com. They are great, and it is a more visual way to build a page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Gatsby is great. I can totally recommend you to build a page with it. But next time you would like to build a project, think about what your purpose is. I mean that's a general question you&lt;br&gt;
should ask yourself when thinking about a project. Do not use the shiny new framework because its new, use something that suits the job.&lt;/p&gt;

&lt;p&gt;I think most of the time Gatsby will do a great job, but sometimes it would be better to use something else. Hopefully my point of view will help you than to achieve your goals and satisfy&lt;br&gt;
your needs.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Build an online Shop with Gatsby and Shopify (Part 3)</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Fri, 28 May 2021 08:18:36 +0000</pubDate>
      <link>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-3-10a6</link>
      <guid>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-3-10a6</guid>
      <description>&lt;p&gt;This is the third and last part of my gatsby and shopify series. If you have not read the parts before I would recommend reading them before. Otherwise, it will be hard to follow.&lt;br&gt;
Today we will add the function to add a product to our cart and then build a checkout page where you can click on checkout to get redirected to your personalized shopify checkout.&lt;/p&gt;

&lt;p&gt;First we should recall where we stopped.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isomorphic-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify-buy&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;storefrontAccessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ACCESS_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;domain&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SHOP_NAME&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="nx"&gt;fetch&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;defaultValues&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="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;lineItems&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&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;isBrowser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&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;setCheckoutItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isBrowser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checkout&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="nf"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initializeCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isBrowser&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;`null`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&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;existingCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt;
          &lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&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;newCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newCheckout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;initializeCheckout&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;StoreContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&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;children&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;/StoreContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add Variant to Cart
&lt;/h2&gt;

&lt;p&gt;Next up we would need a function to go add a product to the cart. So let us build it.&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;addVariantToCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quantity&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;checkoutID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lineItemsToUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;variantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&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="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addLineItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkoutID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lineItemsToUpdate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function takes two arguments. The first one is the id of the product or more specific: the id of the variant. That means when you have a product like a shirt you would have different variants.&lt;br&gt;
You would take the id for the variant. The other parameter is the quantity.&lt;/p&gt;

&lt;p&gt;Then afterwards we get the id for the checkout. This we need for later. It will be used for making our request because shopify needs to know to which checkout they need to add the variant to.&lt;br&gt;
Then we construct or data we want to send. For now, we only send one at a time. Later you could make this more complex and maybe to a bulk adding version of this function.&lt;/p&gt;

&lt;p&gt;In the last step we need to use our shopify sdk to add the items to the checkout. Then we wait for the response and set it to the state.&lt;/p&gt;
&lt;h2&gt;
  
  
  Passing our data
&lt;/h2&gt;

&lt;p&gt;One important thing we missed in our ContextProvider file. We need to pass the functions and our data to use it afterwards.&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="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;StoreContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;addVariantToCart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;,&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="nx"&gt;children&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;/StoreContext.Provider&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;p&gt;We pass an object called value into the context provider. There we spread in the defaultValues. Pass our newly build function and the checkout state.&lt;/p&gt;

&lt;p&gt;Now we could use our provider. 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the provider
&lt;/h2&gt;

&lt;p&gt;In order to make the data accessible everywhere in our pages I would recommend adding the ContextProvider component in our layout. Just wrap everything else in it. This makes it available everywhere else.&lt;/p&gt;

&lt;p&gt;Now we could use the context everywhere you want.&lt;/p&gt;

&lt;p&gt;We will add it to a component which shows a product with the add to cart button.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../provider/ContextProvider&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ProductDescription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;priceRange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shopifyId&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;addVariantToCart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;StoreContext&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;handleAddToCart&lt;/span&gt; &lt;span class="o"&gt;=&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="nf"&gt;addVariantToCart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shopifyId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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;handleDirectCheckout&lt;/span&gt; &lt;span class="o"&gt;=&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="nf"&gt;addVariantToCart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shopifyId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_self&lt;/span&gt;&lt;span class="dl"&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;row&lt;/span&gt;&lt;span class="dl"&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;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;col&lt;/span&gt;&lt;span class="dl"&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;StyledProductName&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;title&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;/StyledProductName&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledPrice&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;priceRange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;minVariantPrice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="err"&gt;€&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledPrice&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledLabel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Product&lt;/span&gt; &lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledLabel&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledSelect&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;producttype&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;producttype&lt;/span&gt;&lt;span class="dl"&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;option&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Download&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/option&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;option&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Artwork&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Artwork&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/option&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledSelect&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;hr&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;StyledProductText&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;description&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;/StyledProductText&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledButton&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mr-3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleAddToCart&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Add&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;Cart&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledButton&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StyledButtonSecondary&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleDirectCheckout&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Direct&lt;/span&gt; &lt;span class="nx"&gt;Checkout&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StyledButtonSecondary&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ProductDescription&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not wonder why the JSX Tags look a bit different. They are styled with styled components. Which is an entirely different topic. If you want to learn more about it. Just have a look here: &lt;a href="https://styled-components.com/"&gt;Styled components&lt;/a&gt;.&lt;br&gt;
For now, you just have to know that it is a way of styling and under the hood you could use any tag you want. And as we want to focus here on building the functionality this is not something you should worry about.&lt;/p&gt;

&lt;p&gt;Enough about styled components. First we need to import the store context. Then we can use it in the component.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;addVariantToCart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;StoreContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the addVariantToCart function and the checkout state from the StoreContext. Above is shown how you get it from the context. Pretty easy right?&lt;/p&gt;

&lt;p&gt;Next We have two functions which will handle the clicks on the buttons. The first just calls the addVariantToCart function with the shopifyId which was passed as props. The id you get with calling the shopify data on your page.&lt;br&gt;
And then you could also add a quantity. For simplicity its only possible to add one product at the moment. But that would be an easy fix and could be done later.&lt;/p&gt;

&lt;p&gt;Next the handleDirectCheckout function. This would be if a user directly wants to check out with the product on the page. Therefore, the addVariantToCart function is also called afterwards we open the shopify checkout.&lt;br&gt;
The url of the checkout is stored in the checkout state. This is a large object we get from shopify. Also, there are other things in it like all the items in the line or the total amount of the cart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;Now you know how to use the context and how to build your own shop with shopify and gatsby. I know that's not all you need to build, but it should give you a starting point and hopefully&lt;br&gt;
enables you to build your own shop. I think it is really fun and a cool way to build your custom shop.&lt;/p&gt;

&lt;p&gt;If you have a question feel free to reach out. I would be happy to help.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>gatsby</category>
      <category>shopify</category>
    </item>
    <item>
      <title>Build an online Shop with Gatsby and Shopify (Part 2)</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Thu, 20 May 2021 09:08:27 +0000</pubDate>
      <link>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-2-dkk</link>
      <guid>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-2-dkk</guid>
      <description>&lt;p&gt;In the &lt;a href="https://www.rileven.tech/blog/gatsby-and-shopify"&gt;last part&lt;/a&gt; we learned how to get data from shopify. This helps us to show the customer our data. But this is only one way. We only can show data and not interact with it.&lt;br&gt;
So what would happen if a user wants to buy an Item from our store. Right now it would not work. But in this series we will make it work. So fasten your seatbelt we will start right now.&lt;/p&gt;
&lt;h2&gt;
  
  
  Shopify Buy SDK
&lt;/h2&gt;

&lt;p&gt;For the interaction you need the shopify buy SDK. I totally recommend you to have a look at the &lt;a href="https://shopify.github.io/js-buy-sdk/"&gt;documentation&lt;/a&gt;. There a lot more things you could do with the SDK than&lt;br&gt;
we can cover here.&lt;/p&gt;

&lt;p&gt;First you need to install the SDK.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;shopify-buy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need isomorphic-fetch. You might know what fetch is, but what is it, and why do we need it? The problem with fetch is that is currently not implemented across all browsers consistently.&lt;br&gt;
Isommorphic-Fetch allows you to fetch in your node code in a cross-browser compliant fashion. So it is better to use it here than fetch.&lt;/p&gt;

&lt;p&gt;So the next step would be to also install this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;isomorphic-fetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to start coding. So grab your favorite drink and lets start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3ogwFMGyyClcWcO4es/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3ogwFMGyyClcWcO4es/giphy.gif" alt="Alt Text" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  React Context
&lt;/h2&gt;

&lt;p&gt;So the first thing would be to initialise the newly installed SDK. But where do we place it? It should be accessible everywhere, and it would be nice if we could abstract the logic to a separate file.&lt;br&gt;
Would the layout component be right? No I guess that would not be good. Maybe you have a page which won't use the layout. Then you could not use the SDK.&lt;/p&gt;

&lt;p&gt;I would recommend using the React context. I will explain shortly what this is. If you are familiar with it feel free to jump to the part where we initialise the SDK.&lt;/p&gt;

&lt;p&gt;The context allows us to pass data through the component tree without having to pass props down to every component by hand. That is awesome because it could be really annoying to pass the props all&lt;br&gt;
the time to your components. You can learn more about it in the official &lt;a href="https://reactjs.org/docs/context.html"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So the next step would be to add a folder called &lt;strong&gt;provider&lt;/strong&gt; with one file in it called &lt;strong&gt;ContextProvider.js&lt;/strong&gt;. At the top you need to import three things.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isomorphic-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify-buy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's now continue with initializing the shopify-buy SDK.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialize the Shopify Buy SDK.
&lt;/h2&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;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;storefrontAccessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ACCESS_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;domain&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SHOP_NAME&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="nx"&gt;fetch&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the build client function we can initialize the client. You have to pass your storefront token and your domain name. And also the isomorphic fetch as a second argument.&lt;br&gt;
Afterwards the client is ready to use.&lt;/p&gt;

&lt;p&gt;Next we should set some default values the application could use to set up the store. Just create a constant under the initialized client.&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;defaultValues&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="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;lineItems&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client is the client we initialized before. And then we also have a checkout object which contains an array of lineItems. This is the array which will store all the items a user puts into his shopping bag.&lt;/p&gt;

&lt;p&gt;The next step would be to create the context and really important export it. We should export it that we could use it in our component tree. But this will be done later but add the line to your file.&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The createContext function was imported from react above and is a function that comes with react. We pass the default values to give create the context with the values we defined. Nothing special here.&lt;/p&gt;

&lt;p&gt;Another helper we add under the const is a check if we are in a browser. Why do we need this? Gatsby is server-side rendered and at some points we may need to access the window element. On the server we wont&lt;br&gt;
have a window object because it comes with the browsers. So the helper show return true if we are in a browser.&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;isBrowser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we check the type of the window property. If window would be undefined we could not access it, and we might be not in the browser. We need later to access the localStorage, and therefore we need&lt;br&gt;
a window object. If we would not check this the operation could potentially fail.&lt;/p&gt;

&lt;p&gt;Let's have a look how your ContextProvider file would look right now.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isomorphic-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify-buy&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;storefrontAccessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ACCESS_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;domain&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SHOP_NAME&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="nx"&gt;fetch&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;defaultValues&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="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;lineItems&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&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;isBrowser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create the Context Component
&lt;/h2&gt;

&lt;p&gt;Awesome now we finally can build the ContextProvider Component with all the logic in it.&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;ContextProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&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;StoreContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&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;children&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;/StoreContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok that's a lot at once. I give my best to explain it as easy as possible. In the first line we create the component and pass a child into the component. The children allow us to use the&lt;br&gt;
component later as a wrapper component. Like so:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextProvider&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;AnyChildComponent&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;/ContextProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we need to pass it somewhere in our return value from the component.&lt;/p&gt;

&lt;p&gt;In the first line of our functional component we defined a State with the &lt;strong&gt;useState&lt;/strong&gt; hook. We pass to properties to it. One is the checkout. With this we could use the values stored in the checkout. And the&lt;br&gt;
others would be setCheckout which allows us to change the values which are store in the checkout. And we pass the hook our default values for the checkout.&lt;/p&gt;

&lt;p&gt;Why is this useful? There may be the case where you want&lt;br&gt;
to access all your items in the shopping cart. But the customer did not put one item in there. So your store would crash because you tried to map over all items in your bag, but there is emptiness inside&lt;br&gt;
and no array you could use for mapping over.&lt;/p&gt;

&lt;p&gt;The last thing we have to talk about here is the return value. We return the StoreContext we create before the component and access the provider from it. That's how we use the context. Don't worry too much about it.&lt;/p&gt;

&lt;p&gt;The next step is to initialize the checkout when we want the shop is mounted. To achieve this we use the useEffect hook from react to be able to do some things when the shop is mounted.&lt;/p&gt;
&lt;h2&gt;
  
  
  Use Effect Hook
&lt;/h2&gt;

&lt;p&gt;The useEffect hook is also a react function which can be used to access the lifecycle of our application.&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="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initializeCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isBrowser&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;`null`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&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;existingCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&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;newCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newCheckout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;initializeCheckout&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again it is a lot and I try my best to explain it for now.&lt;/p&gt;

&lt;p&gt;First we use the useEffect hook which we need to pass a function as the first argument. In this case it is an anonymous arrow function. It basically looks like this () =&amp;gt; {}. Weird syntax I think.&lt;br&gt;
Anyways.&lt;/p&gt;

&lt;p&gt;Next up we want to initialize the checkout. So you might think you could directly write all the code inside the arrow function. That's sadly not that easy. As we talk to the serve we need to write an async function.&lt;br&gt;
Because it could take some time to wait for answer from the server. When we want to do something async in the useEffect hook we need to write is as a separate function. Otherwise, it won't work.&lt;br&gt;
So we should write an arrow function which is marked as async.&lt;/p&gt;

&lt;p&gt;In the first line of the function we check if there is already a checkoutId. The checkoutId is used by shopify to save different shopping bags. Every bag that is created gets a unique id which is&lt;br&gt;
used to access it later and recognize the items a user picked. We do the check because we want to present the user a good experience. Maybe he returns to the page after a few hours and then wants to&lt;br&gt;
buy the items. Then we still could recognize what he picked. Also, if he would switch the sites in our shop he would always use the items in his bag. That would be a bad shopping experience.&lt;/p&gt;

&lt;p&gt;For the check we create a variable where could store the value of the existing checkoutId. First we check if we are in a browser otherwise we cannot access the local storage. If we aren't in a browser it would be set to null.&lt;br&gt;
Then we get the localStorage item by the name we defined.&lt;/p&gt;

&lt;p&gt;Short coffee break to get some energy for the next steps.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/NSjWAf3YuFvZL3UiBs/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NSjWAf3YuFvZL3UiBs/giphy.gif" alt="coffe break" width="478" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok back full off energy.&lt;/p&gt;

&lt;p&gt;We next check if we have a checkoutId, and it also should not be null. Remember if it would be null we would be on the server. Then we know there is already a checkout. So the user put some&lt;br&gt;
things in a basket. Then we need to get the things stored in there. So we need to talk to shopify and get the data. But this potentially could go wrong, so we will wrap it in a try catch block.&lt;/p&gt;

&lt;p&gt;Next we use the shopify SDK to fetch the checkout. We pass the id there and then store it in a constant. We need to use await because it could take some time to talk to the server.&lt;/p&gt;

&lt;p&gt;After we got the answer we need to use another if statement to check it the checkoutId was not already used to check out. If this is not the case we could set the checkout item and return which stops the function.&lt;/p&gt;

&lt;p&gt;The setCheckoutItem is a little helper function which I created. It looks like the following and has to be declared above the useEffect.&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;setCheckoutItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isBrowser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checkout&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="nf"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It takes the checkout obIt takes the checkout object and first stores the checkoutId into the localStorage for later and also sets the checkout state. That's needed because we use it at each new mount of the shop as described earlier.&lt;/p&gt;

&lt;p&gt;Now we need to go back again to the function inside the useEffect hook. At the bottom of the initializeCheckout function are two more lines I would like to point your attention to. If there would not be a checkoutId we have&lt;br&gt;
to create a new checkout. Therefore, we again use the shopify sdk to create it and store it into a constant. And then again call our little helper function. So this is needed when a customer visits our store&lt;br&gt;
for the first time or after a long time again.&lt;/p&gt;

&lt;p&gt;And then we are done with the initalize function. So we need to call it inside the use effect hook. One important thing you should not forget is to add the empty array as second argument to the&lt;br&gt;
useEffect hook. This ensures that the hook is only run on the first render.&lt;/p&gt;

&lt;p&gt;Wow. That was a lot. Here is the file again in total now.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;isomorphic-fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify-buy&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;storefrontAccessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ACCESS_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;domain&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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SHOP_NAME&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="nx"&gt;fetch&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;defaultValues&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="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;lineItems&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&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;isBrowser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&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;setCheckoutItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isBrowser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checkout&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="nf"&gt;setCheckout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initializeCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isBrowser&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;`null`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&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;existingCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;existingCheckoutID&lt;/span&gt;
          &lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completedAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existingCheckout&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shopify_checkout_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&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;newCheckout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nf"&gt;setCheckoutItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newCheckout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;initializeCheckout&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;StoreContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&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;children&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;/StoreContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So that's the end for now. It was a lot to cover, but now we have the foundation for our interaction with the shop setup. In the next steps would be to actually use the context and add items to the shopping bag.&lt;br&gt;
Afterwards we will build a shopping back component. Sounds like fun? Stay tuned for the next part.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>javascript</category>
      <category>react</category>
      <category>shopify</category>
    </item>
    <item>
      <title>Build an online Shop with Gatsby and Shopify (Part 1)</title>
      <dc:creator>Rich11</dc:creator>
      <pubDate>Wed, 19 May 2021 16:42:54 +0000</pubDate>
      <link>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-1-25e3</link>
      <guid>https://dev.to/richardmen11/build-an-online-shop-with-gatsby-and-shopify-part-1-25e3</guid>
      <description>&lt;p&gt;I wanted to build an online shop but it should not be the typical shopify design. Otherwise it would be nice to have the functionallity of shopify.&lt;br&gt;
Ok maybe I could build something with Liquid (shopifys own templating language). But I already now react and gatsby, why should I learn something new?&lt;/p&gt;

&lt;p&gt;Ok maybe you feel the same. Maybe not. This article should give you a small guideline on how to start with Gatsby and shopify. It should help&lt;br&gt;
you to build your first custom online shop. Its easier than you think.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a new Gatsby Project
&lt;/h2&gt;

&lt;p&gt;Yes thats the start. Thankfully gatsby offers a simple way to setup a new project. You can use their own CLI wich enables you to run and create projects.&lt;br&gt;
Just install it via npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; gatsby-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The -g means global which installs the package globally which makes it available everywhere. If you want to learn more about the CLI you should have a look &lt;a href="https://www.gatsbyjs.com/docs/reference/gatsby-cli/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The next step is to generate the project. Move to the folder which suits your project the most and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gatsby new my-default-starter https://github.com/gatsbyjs/gatsby-starter-default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a project with a default starter template. You could also choose a different starter or go through the process. But I would recommend&lt;br&gt;
to understand how the ins and outs of gatsby work until you use a prebuild starter.&lt;/p&gt;

&lt;p&gt;Next you could start your page with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gatsby develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should serve the project on &lt;a href="https://localhost:8000"&gt;https://localhost:8000&lt;/a&gt;. Next up we will add the shopify data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get the data from shopify
&lt;/h2&gt;

&lt;p&gt;To get the data from shopify we need a plugin to get the data from shopify. I would recommend that you have a look at &lt;a href="https://github.com/gatsbyjs/gatsby-source-shopify"&gt;offical documentation&lt;/a&gt;.&lt;br&gt;
The Plugin could be installed by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; gatsby-source-shopify
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you installed the plugin you have to change your gatsby-config.js. No worries. We will do it together.&lt;br&gt;
Just add the entrie below to your plugins array.&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`gatsby-source-shopify`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// The domain name of your Shopify shop.&lt;/span&gt;
        &lt;span class="nl"&gt;shopName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// The storefront access token&lt;/span&gt;
        &lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The both empty values have to be filled from you. I will show you where to find the data and how to put them in. The process is relatively easy.&lt;br&gt;
One thing to mention here: I would recommend to add these credentials via environment variables. That keeps them secure and helps to keep your data safe.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create your store
&lt;/h2&gt;

&lt;p&gt;If you dont have already setup a store here are some basics tipps to set it up. One thing that most people forget that you have to pay for shopify. But there&lt;br&gt;
is a workaround for that. First create an account at the &lt;a href="https://www.shopify.com/partners"&gt;shopify partners page&lt;/a&gt;. Its a tool for agencies or people who build stores for others.&lt;br&gt;
There you can create an account and setup a shopify shop which is for free until you transfer the ownership or want to sell products. It means you can take the time&lt;br&gt;
to build your site and don´t need to worrie about payments in the beginning.&lt;/p&gt;

&lt;p&gt;After you logged in into your shop go to apps. And click on manage private apps at the bottom of the page. Then create a new private app. Add the name of your app and give the app a name. Make sure to&lt;br&gt;
allow the access to the Shopify Storefront API. Then save the app and copy your access token.&lt;/p&gt;

&lt;p&gt;You can now add the credentials to the gatsby-config file we edidted before. Add the access token and the shopName and please dont commit your credentials to github.&lt;/p&gt;
&lt;h2&gt;
  
  
  Querying your products
&lt;/h2&gt;

&lt;p&gt;If you already have products in your shop you now should be able to get them through the plugin. Simply start your page with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gatsby develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and check the GraphiQL Playground under &lt;a href="http://localhost:8000___graphql"&gt;http://localhost:8000___graphql&lt;/a&gt;. There you should now be a lot more queries available than before. If not please&lt;br&gt;
check if your credentials are correct.&lt;/p&gt;

&lt;p&gt;For example you now could build a query like this here:&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pageQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="s2"&gt;`
  query($id: String!) {
    product: shopifyProduct(id: { eq: $id }) {
      description
      title
    }
  }
`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query gets only a product with a specific id with its title and description. The data coming back from the query would be available as data props to your page.&lt;/p&gt;

&lt;p&gt;Now you were able to get your first product data from shopify. The next step would be to get the data and build a custom shopping cart. This topic will be in another post which will be published soon.&lt;/p&gt;

&lt;p&gt;All in all its really easy and fun to build a shop with gatsby and shopify. I would be happy if you give it a try.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
