<?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: Carlos Eduardo</title>
    <description>The latest articles on DEV Community by Carlos Eduardo (@ehduardu).</description>
    <link>https://dev.to/ehduardu</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%2F327076%2F349ea18b-3de3-4ae7-9e25-a61391258e22.jpg</url>
      <title>DEV Community: Carlos Eduardo</title>
      <link>https://dev.to/ehduardu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehduardu"/>
    <language>en</language>
    <item>
      <title>GAS Web App with React</title>
      <dc:creator>Carlos Eduardo</dc:creator>
      <pubDate>Thu, 10 Sep 2020 00:34:08 +0000</pubDate>
      <link>https://dev.to/ehduardu/gas-web-app-with-react-110g</link>
      <guid>https://dev.to/ehduardu/gas-web-app-with-react-110g</guid>
      <description>&lt;p&gt;If you use Google Apps Script to create Web Apps and are thinking about how to use React for it, you have come to the right place!&lt;/p&gt;

&lt;p&gt;I'm sharing with you a base project so you can create your GAS Web Apps using the React library on the front-end. I used as a base this project &lt;a href="https://github.com/enuchi/React-Google-Apps-Script" rel="noopener noreferrer"&gt;here&lt;/a&gt; that uses React to create Modals in a spreadsheet and I adapted it to use in Web Apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  First steps
&lt;/h2&gt;

&lt;p&gt;You can start by cloning the &lt;a href="https://github.com/ehduardu/apps-script-react-base-project" rel="noopener noreferrer"&gt;repository&lt;/a&gt; to your local machine and run the command: &lt;code&gt;npm&lt;br&gt;
&lt;br&gt;
 install&lt;br&gt;
&lt;br&gt;
&lt;/code&gt; You'll need to use clasp to deploy after you build the application, know how to use clasp &lt;a href="https://github.com/google/clasp" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With npm installed and the clasp configured, copy the script ID of your project into GAS and paste it into the .clasp.json file. &lt;/p&gt;
&lt;h2&gt;
  
  
  Project structure
&lt;/h2&gt;

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

&lt;p&gt;The project structure is basically divided between Client and Server, where one corresponds to the front-end(pages, components...) and the other to the back-end(GAS functions), respectively.&lt;/p&gt;

&lt;p&gt;In the server folder, put all your functions that will perform your script actions, I use the Controller folder to better organize my project.  &lt;/p&gt;

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

&lt;p&gt;Then you must map the functions to be called by the front-end in the file &lt;strong&gt;index.js&lt;/strong&gt;, using global variables. Below I map the &lt;strong&gt;getScriptToken&lt;/strong&gt; function as an example exported from a controller and the &lt;strong&gt;doGet&lt;/strong&gt; function, it's as mandatory in Web Apps (this function will call the file &lt;strong&gt;app.html&lt;/strong&gt; that will be built at the end). &lt;/p&gt;

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

&lt;p&gt;Well, this is the server part of the Web App, but let's now go to the Front-end part which is what interests this post. On the client side, I created the folder &lt;strong&gt;pages&lt;/strong&gt; where I save all the pages of my application and the folder &lt;strong&gt;utils&lt;/strong&gt; where I save the file &lt;strong&gt;server.js&lt;/strong&gt; (which will serve to call the global variables created in the server folder) and any other utility file.&lt;/p&gt;

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

&lt;p&gt;The file &lt;strong&gt;index.html&lt;/strong&gt; is our Web App frame, it contains a session with "index" id, where we will render our React content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fai6t46wkg1jgma9do1oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fai6t46wkg1jgma9do1oj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;em&gt;index.html&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the file &lt;strong&gt;Router.js&lt;/strong&gt; is where we configure our routes, importing the pages and using the elements of the &lt;strong&gt;react-router-dom&lt;/strong&gt;, this way we can navigate between pages through history, since the GAS Web Apps have no URL change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F53g2fso979536eztvm60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F53g2fso979536eztvm60.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;em&gt;Router.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And finally in the file &lt;strong&gt;App.jsx&lt;/strong&gt; the routes of React are rendered in the html file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8i7iaa23by50ty2zixue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8i7iaa23by50ty2zixue.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;em&gt;App.jsx&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I created two pages to perform a basic test, the page &lt;strong&gt;Home&lt;/strong&gt; and the page &lt;strong&gt;Register&lt;/strong&gt;. We can navigate through the pages through a button that calls the "useHistory" element of the &lt;strong&gt;react-router-dom&lt;/strong&gt;. On the page &lt;strong&gt;Register&lt;/strong&gt; I show you how to call server function &lt;strong&gt;getScriptToken&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsowa5x3q5ow7r012vx43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsowa5x3q5ow7r012vx43.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;em&gt;index.js from Page Home&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxb5dlos4012ynlnrw0t4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxb5dlos4012ynlnrw0t4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;em&gt;index.js from Page Register&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After creating your pages and configuring your routes, run the command below to build and deploy in your GAS project.```&lt;br&gt;
&lt;br&gt;
 npm run deploy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Now you have a Web App with React support, use your creativity to create multiple pages, components and install libraries like StyledComponents, which I'm even using in the pages, and several others, but this is subject for the next post, thanks!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kzz1vgu5oma63yoon430.gif)

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

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>google</category>
      <category>googleappsscript</category>
    </item>
  </channel>
</rss>
