<?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: Robert Ruiz</title>
    <description>The latest articles on DEV Community by Robert Ruiz (@robeartoe).</description>
    <link>https://dev.to/robeartoe</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%2F169401%2F0c2bb5e0-b3b7-498a-a5b5-e3468d51471d.jpeg</url>
      <title>DEV Community: Robert Ruiz</title>
      <link>https://dev.to/robeartoe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robeartoe"/>
    <language>en</language>
    <item>
      <title>How to create a slack command with Deno, Cloud Run &amp; Cloud Build!</title>
      <dc:creator>Robert Ruiz</dc:creator>
      <pubDate>Thu, 24 Sep 2020 00:08:05 +0000</pubDate>
      <link>https://dev.to/robeartoe/how-to-create-a-slack-command-with-deno-cloud-run-cloud-build-aib</link>
      <guid>https://dev.to/robeartoe/how-to-create-a-slack-command-with-deno-cloud-run-cloud-build-aib</guid>
      <description>&lt;h1&gt;
  
  
  Introduction:
&lt;/h1&gt;

&lt;p&gt;Do you and your team use slack? You may have seen those slack commands. Those really cool commands that you type out, add some information, and it does some &lt;strong&gt;magic&lt;/strong&gt;. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pQ4hBwAN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a6i6tes2mwcrkqm1fh98.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pQ4hBwAN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a6i6tes2mwcrkqm1fh98.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've ever wanted to make your own slack command, this series will teach you how to. This series is also useful if you want to learn what Deno is all about, getting started with Cloud Run, or seeing Cloud Build in action. &lt;/p&gt;

&lt;p&gt;With that being, said, I hope you enjoy the series!&lt;/p&gt;
&lt;h1&gt;
  
  
  What is Shout:
&lt;/h1&gt;

&lt;p&gt;Let me ask you this. Have you ever been in a situation where you're on slack, and you're in a debate, or a conversation and you really want to get your point across? Explaining your point through different ways &lt;strong&gt;&lt;em&gt;could&lt;/em&gt;&lt;/strong&gt; help, but don't you want to show how passionately you feel about your point? Why not shout it out? IT WILL GET THE POINT ACROSS!&lt;/p&gt;

&lt;p&gt;That's the point of this project, it's a fun little project where you can use a slack command to shout your point across in a fun kind of way.&lt;/p&gt;

&lt;p&gt;The plan for this project is to get it running in Deno, deploy it with cloud run, hook it up to slack,  and setup a CI/CD deployment with cloud build. &lt;/p&gt;
&lt;h1&gt;
  
  
  Deno:
&lt;/h1&gt;

&lt;p&gt;You might be wondering, what exactly is Deno? &lt;a href="https://deno.land/"&gt;Deno&lt;/a&gt; is a runtime environment. Much like Node.js. Actually, it was created by the same person who made Node.js, &lt;a href="https://en.wikipedia.org/wiki/Ryan_Dahl"&gt;Ryan Dahl&lt;/a&gt;. Ryan wants to correct the mistakes he made with Node.js. He talked about his regrets in a talk in 2018, &lt;a href="https://medium.com/@imior/10-things-i-regret-about-node-js-ryan-dahl-2ba71ff6b4dc"&gt;10 Things I Regret About Node.js&lt;/a&gt; Feel free to take a look at it here.&lt;/p&gt;

&lt;p&gt;It can run both Typescript &amp;amp; Javascript. Typescript runs right out of the box! No setup required with that. I'm a huge fan of Typescript so that is great to see.&lt;/p&gt;
&lt;h1&gt;
  
  
  Getting Started:
&lt;/h1&gt;

&lt;p&gt;First thing is first, we need to &lt;a href="https://deno.land/manual/getting_started/installation"&gt;install Deno&lt;/a&gt;.&lt;br&gt;
After it completes, feel free to open a terminal and test it out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, in order for things in this particular project to run smoothly. We will have to install a specific version of Deno. Deno is moving quick, and a lot of times, the latest version of Deno won't work with the latest version of packages. So keeping specific versions ensure everything will work as intended. With that being said, we will want to install version &lt;strong&gt;1.3.2&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno upgrade &lt;span class="nt"&gt;--version&lt;/span&gt; 1.3.2
deno &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make sure deno is good to go, and we're ready to move on!&lt;/p&gt;

&lt;p&gt;Also, I am using yarn (Node.JS package manager). However I'm only using it for running scripts. So installing it is not required, however it's convenient.&lt;/p&gt;
&lt;h1&gt;
  
  
  Code:
&lt;/h1&gt;

&lt;p&gt;Let's get started! Let's start with a simple server. &lt;br&gt;
Create a file called: main.ts, and add these bits of code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Application, Router} from 'https://deno.land/x/oak@v6.0.1/mod.ts';
import bodyParser from './body-parser.ts';
import 'https://deno.land/x/dotenv/mod.ts';

const app = new Application();
const PORT = Deno?.env?.get('PORT') ?? 8080;
const router = new Router();

/**
 * Submit text to be shouted
 *
 * @example
 * curl -X POST "https://localhost:8080/" --data '{"text":"TEST"}'
 * https://api.slack.com/interactivity/slash-commands#app_command_handling
 * @param {object} req request object.
 * @param {object} req.body The request payload.
 * @param {string} req.body.text The user's text.
 * @param {object} res response object.
 */
router
    .post('/', async (context) =&amp;gt; {
      try {
        // Get the Body:
        const body = await context.request.body();
        if (!context.request.hasBody) {
          context.response.status = 400;
          context.response.body = {
            success: false,
            message: 'No data provided',
          };
          return;
        }
         const json = await context.request.body({type: "json"}).value;
        const result = await FetchShoutCloud(json.text! as string);

        context.response.status = 200;
        context.response.type = 'application/json';
        context.response.body = {
          'text': `${result}`,
        };
        return;
      } catch (error) {
        console.error(error);
        context.response.status = 500;
        return;
      }
    });

async function FetchShoutCloud(command: string): Promise&amp;lt;string&amp;gt; {
  const data = await (await fetch('HTTP://API.SHOUTCLOUD.IO/V1/SHOUT', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({'INPUT': command}),
  })).json();
  return data.OUTPUT;
}

app.use(router.routes());
app.use(router.allowedMethods());

app.addEventListener("listen", ({hostname, port, secure}) =&amp;gt; {
    console.log(
      `Listening on: ${secure ? "https://" : "http://"}${
        hostname ?? "localhost"
      }:${port}`
    );
  })

await app.listen({port: Number(PORT)});

export default app;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Whoosh that's a lot of code! Let's break this chunk of code down so we can understand what is happening here.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Application, Router} from 'https://deno.land/x/oak@v6.0.1/mod.ts';
import bodyParser from './body-parser.ts';
import 'https://deno.land/x/dotenv/mod.ts';

const app = new Application();
const PORT = Deno?.env?.get('PORT') ?? 8080;
const router = new Router();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;At the very top of the file we need to import a few packages.&lt;/p&gt;

&lt;p&gt;Traditionally with Node.JS or a frontend application like React or Angular, a package.json will be required, with dependencies that'll be downloaded into a node_packages folder. The project will use those files to utilize any packages. However, there is no dependencies within package.json, or node_packages at all. So how does Deno do it? Also, why is the location of the imported packages from a url, rather than a package folder?&lt;/p&gt;

&lt;p&gt;Well, simply put, deno &lt;em&gt;&lt;strong&gt;has no package manager&lt;/strong&gt;&lt;/em&gt;. It downloads packages (sometimes with specific versions) from that url, and caches the package into a global directory. Reducing the need for a massive node_packages folder for each and every single project you work on. Pretty neat right?&lt;/p&gt;

&lt;p&gt;We'll use that, to import three packages: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bodyParser from the local repository. Note: This will be used at a future post. This is necessary for receiving slack commands from a POST request.&lt;/li&gt;
&lt;li&gt;dotenv to fetch environment variables.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/oakserver/oak"&gt;Oak&lt;/a&gt;, the web server we will be using.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Oak is essentially the Deno version of Node JS's &lt;a href="https://koajs.com/"&gt;Koa&lt;/a&gt; framework. So if you've used this before, you're going to be in a familiar place. Regardless, it's a beginner friendly framework.&lt;/p&gt;

&lt;p&gt;The next few lines we are going to start up an oak application. After that we will create a router, which creates middleware that allows us to create routes (based on the pathname of the request).&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router
    .post('/', async (context) =&amp;gt; {
      try {
        // Get the Body:
        const body = await context.request.body();
        if (!context.request.hasBody) {
          context.response.status = 400;
          context.response.body = {
            success: false,
            message: 'No data provided',
          };
          return;
        }
        const json = await context.request.body({type: "json"}).value;
        const result = await FetchShoutCloud(json.text! as string);

        context.response.status = 200;
        context.response.type = 'application/json';
        context.response.body = {
          'text': `${result}`,
        };
        return;
      } catch (error) {
        console.error(error);
        context.response.status = 500;
        return;
      }
    });

async function FetchShoutCloud(command: string): Promise&amp;lt;string&amp;gt; {
  const data = await (await fetch('HTTP://API.SHOUTCLOUD.IO/V1/SHOUT', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({'INPUT': command}),
  })).json();
  return data.OUTPUT;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The one route we will implement is a POST request to the base url. We will fetch the body of the request. If there is no body, we'll return an error. If there is a body, we'll parse through the contents, and pass the results onto an API called ShoutCloud. They handle the capitalization of the request. Finally, we get the results from that API request, and return back the results in the form of a JSON response.&lt;/p&gt;

&lt;p&gt;If anything happens in the middle of this process, it will throw an error, and return back a 500 error.&lt;/p&gt;

&lt;p&gt;What does the context mean? The context represents the current request that's going through Oak's middleware. It is very useful when you have multiple middleware functions passing through.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(router.routes());
app.use(router.allowedMethods());

app.addEventListener("listen", ({hostname, port, secure}) =&amp;gt; {
    console.log(
      `Listening on: ${secure ? "https://" : "http://"}${
        hostname ?? "localhost"
      }:${port}`
    );
  })

await app.listen({port: Number(PORT)});
export default app;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lastly, we have to tell the Oak application to use the router middleware. We will return middleware that does all the route processing. The second line will handle methods that are allowed. If none of the routes handle a certain method, then a 405 error will be returned! Meaning if the user attempts to use a GET request rather than a POST request, they'll get a 405 error.&lt;/p&gt;

&lt;p&gt;The next block is more for us, it's when the application starts and beginnings listening to request. We will have some feedback on the console knowing whether the application or not.&lt;/p&gt;

&lt;p&gt;Finally, the application begins to listen for requests!&lt;/p&gt;
&lt;h1&gt;
  
  
  Running:
&lt;/h1&gt;

&lt;p&gt;We are almost there. Now it's time to run the file!&lt;br&gt;
Type in the command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deno run &lt;span class="nt"&gt;--allow-env&lt;/span&gt; &lt;span class="nt"&gt;--allow-net&lt;/span&gt; ./main.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By default Deno works in a &lt;strong&gt;sandbox&lt;/strong&gt; environment. That means it has no permissions by default unless you explicitly allow those permissions. In this case we're allowing two permissions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;--allow-env: Allows us to access environment variables.&lt;/li&gt;
&lt;li&gt;--allow-net: Allows us to access the internet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should see in your terminal, that the application is listening on localhost:8080/&lt;/p&gt;

&lt;p&gt;It's up and running!&lt;/p&gt;
&lt;h1&gt;
  
  
  Testing:
&lt;/h1&gt;

&lt;p&gt;Now that we got it running, let's test it out! How do we test out a POST request though? We could do it through the terminal, but let's make things much easier. &lt;/p&gt;

&lt;p&gt;Let's use an API explorer called &lt;a href="https://insomnia.rest/"&gt;Insomnia&lt;/a&gt;. Specifically &lt;a href="https://insomnia.rest/download/"&gt;Insomnia Core&lt;/a&gt;. It's an easy way to explore restful APIs &amp;amp; GraphQL APIs.&lt;/p&gt;

&lt;p&gt;Once it's installed, open it up and create a new POST request. You can create a new request with the plus icon near the top left.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s2GBDhpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ycn6ep26761nzf4nhaeo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s2GBDhpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ycn6ep26761nzf4nhaeo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set a name, set the method from GET to POST. And at the right, we'll set the body to JSON.&lt;/p&gt;

&lt;p&gt;Once you create the new request add this url in the url field:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8080/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now within the body tab, we have to input some data.&lt;br&gt;
Paste the following into the text field:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="s2"&gt;"text"&lt;/span&gt;: &lt;span class="s2"&gt;"hello!"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9fmAQCLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dpz4olay60zne3rj8j0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9fmAQCLf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dpz4olay60zne3rj8j0m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
After that you can click send, and you should see your result in the right!&lt;br&gt;
If you receive a &lt;strong&gt;500&lt;/strong&gt; error, ensure your application is on! If you receive a &lt;strong&gt;403&lt;/strong&gt; error, ensure your url is correct.&lt;/p&gt;

&lt;p&gt;If all went well, you should be able to see your text! Nice work!&lt;/p&gt;
&lt;h1&gt;
  
  
  Thoughts on Deno:
&lt;/h1&gt;

&lt;p&gt;Deno v1.0.0 was released on May 12, 2020. It is in a stable state after 2 years of development. That being said, there are still a lot of growing pains. &lt;/p&gt;

&lt;p&gt;Deno, and a lot of popular packages are growing, and iterating, fast. There is a lot of specific versions of Deno, and specific versions of packages you need to install, or else the application won't compile due to a type errors w/in Deno. This part was very confusing, and took a lot of research to figure out which version was compatible with what.&lt;/p&gt;

&lt;p&gt;Overall, Deno is really neat. I don't quite see it surpassing Node.js yet, especially since things are rapidly evolving at a lightning fast pace, and things aren't too stable. However I will definitely use it in the future for production projects. The community is growing, and I wouldn't mind being apart of that. The Deno contributors, and the community was so nice in answering all of my questions I've had throughout this project.&lt;/p&gt;

&lt;p&gt;Perhaps Deno will be the next standard. Only time can tell.&lt;/p&gt;
&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;That's all for part one. In the next parts we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get it up and running with Cloud Run.&lt;/li&gt;
&lt;li&gt;Setup a Slack Developer account &amp;amp; Slack Application&lt;/li&gt;
&lt;li&gt;And finally, automate the deployments with cloud build.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any questions feel free to leave a comment out below or reach out to me.&lt;/p&gt;

&lt;p&gt;The full Github repository can be found here:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3JOwpme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/robeartoe"&gt;
        robeartoe
      &lt;/a&gt; / &lt;a href="https://github.com/robeartoe/shout"&gt;
        shout
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A slack command, made with Google Cloud Run and the SHOUTCLOUD package.
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>deno</category>
      <category>gcp</category>
      <category>cloudrun</category>
      <category>slack</category>
    </item>
    <item>
      <title>Building your own React hooks in Typescript </title>
      <dc:creator>Robert Ruiz</dc:creator>
      <pubDate>Wed, 27 May 2020 01:37:25 +0000</pubDate>
      <link>https://dev.to/robeartoe/building-your-own-react-hooks-in-typescript-492j</link>
      <guid>https://dev.to/robeartoe/building-your-own-react-hooks-in-typescript-492j</guid>
      <description>&lt;p&gt;React Hooks has been a feature since version 16.8. A feature that has greatly influenced the way I develop in React. Moving from class components to functional components.&lt;/p&gt;

&lt;p&gt;However, what are hooks? How to we create our own hooks? We will address all of these questions. All in Typescript. Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Hooks?
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://reactjs.org/docs/hooks-overview.html#but-what-is-a-hook" rel="noopener noreferrer"&gt;reactjs.org&lt;/a&gt;, Hooks, "are functions that let you "hook into" React state, and lifecycle features from function components." So instead of having to do react components with classes, you can do it from functions! &lt;/p&gt;

&lt;p&gt;If you had to write function components, and needed state, it needs to be converted into classes. Now you can do it all in your function components!&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we create our own hooks?
&lt;/h2&gt;

&lt;p&gt;There may be times when we have some logic that would be perfect for multiple components.&lt;/p&gt;

&lt;p&gt;We don't want to copy and paste the same logic within all the components. So what we can do instead, is to extract the logic. And move it to its own function! After we do that, any component that may need the logic, can import it in, and use it. Making things nice and tidy in your code!&lt;/p&gt;

&lt;p&gt;Every custom hook will have a name that starts with 'use', so React can determine that it's a hook. We can also specify what arguments, and what it should return. If any. So, it's pretty much a normal function!&lt;/p&gt;

&lt;h2&gt;
  
  
  Example:
&lt;/h2&gt;

&lt;p&gt;Here's an example. This is a web application, that renders a simple page.&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%2Fphdck8m766c4uuwseel1.gif" 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%2Fphdck8m766c4uuwseel1.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time we change the height or width of the window, the new resolution will be displayed onto the page. And it will display the number of re-renders it did!&lt;/p&gt;

&lt;p&gt;This was all possible with React Hooks! Let's look at the main page component.&lt;br&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%2Fvb6qm98uqkecw81u0alj.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%2Fvb6qm98uqkecw81u0alj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
It sets a local hook within the page, called &lt;em&gt;renders&lt;/em&gt; and, gets the width, and height of the screen dimensions from the useDimenions hook.&lt;/p&gt;

&lt;p&gt;useEffect is a function that works hand in hand with hooks. This code is saying, if height or width changes, then update the number of renders.&lt;/p&gt;

&lt;p&gt;useResolution Hook:&lt;br&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%2Fq5zr80ph548zv7hendrm.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%2Fq5zr80ph548zv7hendrm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
This part was a learning process for me. I had a bit of trouble with the event listeners. However, user QoP from stackoverflow provided a great solution for this problem.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&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%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs/36862446#36862446" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: Get viewport/window height in ReactJS
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Apr 26 '16&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs/36862446#36862446" rel="noopener noreferrer"&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%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          800
        &lt;/div&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%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;&lt;strong&gt;Using Hooks (React &lt;code&gt;16.8.0+&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Create a &lt;code&gt;useWindowDimensions&lt;/code&gt; hook.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import { useState, useEffect } from 'react'
function getWindowDimensions() {
  const { innerWidth: width, innerHeight: height } = window;
  return {
    width,
    height
  };
}

export default function useWindowDimensions() {
  const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions());

  useEffect(() =&amp;gt; {
    function handleResize() {&lt;/code&gt;&lt;/pre&gt;…
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs/36862446#36862446" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;
Huge shoutout to this person for sharing a working example of how to get the resolution with React hooks.

&lt;p&gt;Essentially though, when the hook is used for the first time, it sets an event listener to the window. It will call handleResize when the dimensions change. We then get the width and height of the window, and set the values in the windowDimensions hook. Finally, we return windowsDimensions. We can then use this hook anywhere where this is needed!&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;I hope this article helped you learn a bit more about React hooks, and custom React hooks. If you have any questions or anything you may need followup with. Please let me know!&lt;/p&gt;
&lt;h2&gt;
  
  
  Repository:
&lt;/h2&gt;

&lt;p&gt;Feel free to view, fork, or clone the repository.&lt;br&gt;
If you have any issues, or any questions, feel free to let me know!&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/robeartoe" rel="noopener noreferrer"&gt;
        robeartoe
      &lt;/a&gt; / &lt;a href="https://github.com/robeartoe/ReactHooks" rel="noopener noreferrer"&gt;
        ReactHooks
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A small example of creating custom React hooks!
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
