<?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: Hunter Becton</title>
    <description>The latest articles on DEV Community by Hunter Becton (@hunterbecton).</description>
    <link>https://dev.to/hunterbecton</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%2F195860%2F1f63dc8e-a0f4-4b18-a2e1-15a8de0caa4b.png</url>
      <title>DEV Community: Hunter Becton</title>
      <link>https://dev.to/hunterbecton</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hunterbecton"/>
    <language>en</language>
    <item>
      <title>Middleware in Next.js: Moving from Express</title>
      <dc:creator>Hunter Becton</dc:creator>
      <pubDate>Mon, 15 Feb 2021 19:01:59 +0000</pubDate>
      <link>https://dev.to/hunterbecton/middleware-in-next-js-moving-from-express-1bmf</link>
      <guid>https://dev.to/hunterbecton/middleware-in-next-js-moving-from-express-1bmf</guid>
      <description>&lt;p&gt;I'm currently moving my site to Next.js from a site that used an Express backend. I'm a huge fan of Express, and one of the things I love about Express is how simple it is to implement middleware.&lt;/p&gt;

&lt;p&gt;Middleware is a function that you can attach to routes in order for it to run before the route. In other words, it runs in "the middle." This is useful for things like checking if a user is authenticated or has the proper roles to access a route. &lt;/p&gt;

&lt;p&gt;These are features you would want on multiple routes, and middleware makes it easy to write the function in one place and reuse it across multiple routes (Don't Repeat Yourself). But what about Next.js–how do you implement middleware?&lt;/p&gt;

&lt;p&gt;Well, you could &lt;a href="https://nextjs.org/docs/advanced-features/custom-server"&gt;create a custom Express server&lt;/a&gt; to work with Next.js, but you're losing some of the benefits of creating your API the way Next.js intended. &lt;/p&gt;

&lt;p&gt;Instead, you can watch how I implement middleware in my Next.js application in the video below, or for those that rather read &lt;strong&gt;I have a written explanation below the video&lt;/strong&gt;–enjoy! 😊&lt;/p&gt;

&lt;p&gt;Code: &lt;a href="https://github.com/hunterbecton/next-js-middleware"&gt;https://github.com/hunterbecton/next-js-middleware&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XbvnxKzVXks"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Before: My Express Protect Middleware
&lt;/h2&gt;

&lt;p&gt;In Express I have all my routes and middleware associated with authentication inside an &lt;code&gt;authController.js&lt;/code&gt; file. One of those middleware functions is the &lt;code&gt;protect&lt;/code&gt; function, which you can see in the code below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This protect function would check to see if the &lt;code&gt;req&lt;/code&gt; had cookies and if the &lt;code&gt;st_accessToken&lt;/code&gt; cookie was present. If so, it would attach the token to the &lt;code&gt;token&lt;/code&gt; variable defined at the top of the function.&lt;/p&gt;

&lt;p&gt;If no token was present my application would return an error asking the user to log in, so the user would never reach the final route. If a token was present the function would then proceed to run some code in a &lt;code&gt;try / catch&lt;/code&gt; block. &lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;try&lt;/code&gt; block the token would be decoded using the &lt;code&gt;jwt.verify&lt;/code&gt; method from the &lt;code&gt;jsonwebtoken&lt;/code&gt; package. This method is passed the token and my application's JWT secret. Once that's decoded my application has access to the user's unique ID on &lt;code&gt;decoded.id&lt;/code&gt;. This user ID is then used to make a call with Mongoose to my MongoDB database: &lt;code&gt;const currentUser = await User.findById(decoded.id);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If no user if found the application will return an error, otherwise the function will attach a &lt;code&gt;user&lt;/code&gt; object on &lt;code&gt;req&lt;/code&gt; based on the &lt;code&gt;currentUser&lt;/code&gt; that came back from MongoDB. Last, unless an error is caught in the &lt;code&gt;catch&lt;/code&gt; block, the function calls &lt;code&gt;next()&lt;/code&gt;, which tells Express to move to the next route handler or middleware.&lt;/p&gt;

&lt;p&gt;Now routes or middleware further down the chain will have access to the user object and can use it however they want. For example, my routes for Stripe will now be able to read the Stripe Customer ID from the user object that is attached to &lt;code&gt;req.user&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This middleware is implemented in Express when I create my routes in the &lt;code&gt;userRoutes.js&lt;/code&gt; file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Now: My Next.js withProtect Middleware
&lt;/h2&gt;

&lt;p&gt;In Next.js you can create your routes in a folder called &lt;code&gt;api&lt;/code&gt;, which needs to be inside the &lt;code&gt;pages&lt;/code&gt; folder. Then, inside your &lt;code&gt;api&lt;/code&gt; folder you can create all your route handlers, nesting them inside other folders based on how you want your API to be organized. Next.js will handle creating the routes for you, so there's no need to define them like you would in Express.&lt;/p&gt;

&lt;p&gt;For example, a &lt;code&gt;logout.js&lt;/code&gt; handler inside &lt;em&gt;pages &amp;gt; api &amp;gt; users &amp;gt; logout&lt;/em&gt; can be accessed in development from &lt;code&gt;localhost:3000/api/users/logout&lt;/code&gt;. Pretty neat, right? &lt;/p&gt;

&lt;p&gt;However, because Next.js handles the routing for us, we can't just pass a middleware before the route is called when we define routes ourselves in Express. So now let's look at the withProtect middleware code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Look familiar? That's because it's almost identical to the protect middleware in Express! However, there are some important things to point out. To make them easier to see, check out this code example below where I remove some of the identical code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can now see how this withProtect middleware takes the handler in as an argument, then returns a function of &lt;code&gt;(req, res)&lt;/code&gt;. This essentially takes over the handler for now, before it later passes it back into the original handler when &lt;code&gt;handler(req,res)&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;Now with the withProtect middleware complete, it's time to implement it inside the logout route. Refer to the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Inside the logout handler you can see that it has access to &lt;code&gt;req.user&lt;/code&gt;, which is passed on by the withProtect middleware. &lt;/p&gt;

&lt;p&gt;So how does the logout handler get the access? Well, if you look at the bottom of the code you will see that I wrapped the exported handler with the withProtect middleware–identical to how you would do higher order components in React: &lt;code&gt;export default withProtect(handler);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By doing this, the withProtect middleware will run before the logout handler and give the logout handler the user object on &lt;code&gt;req.user&lt;/code&gt;, unless there is an error and the middleware will return an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chaining Multiple Middlewares in Next.js
&lt;/h2&gt;

&lt;p&gt;What happens if you want to add multiple middleware on a handler? Simple, you just nest it inside the other middlewares!&lt;/p&gt;

&lt;p&gt;For example, check out this withRoles middleware:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This middleware takes two arguments: the original handler function and a string of possible roles that can access the route. &lt;/p&gt;

&lt;p&gt;Users are assigned the role of &lt;code&gt;user&lt;/code&gt; in my database by default when they're created. This is important because there are some routes, like deleting a user, that I only want users with the role of &lt;code&gt;admin&lt;/code&gt; to access. &lt;/p&gt;

&lt;p&gt;This withRoles middleware gets access to the user on &lt;code&gt;req.user&lt;/code&gt; because it's nested inside the withProtect middleware. Although it doesn't make much sense to only allow admins to logout, check out this simplified example of the logout handler:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First, the withProtect middleware runs, either attaching the user object on &lt;code&gt;req.user&lt;/code&gt; or returning an error. Then the withRoles middleware checks to see if the req.user.role matches 'admin'. If it does, the logout handler is then called. Otherwise, the middleware returns a response notifying the user that they do not have the proper permissions.&lt;/p&gt;

&lt;p&gt;Nesting middleware like this can look a little weird compared to how you would implement it in Express, but once you get the hang of it the implementation in Next.js is simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Found this helpful? &lt;a href="https://www.youtube.com/channel/UCvHKiUI75ytqUcN851fRR2w?sub_confirmation=1"&gt;Subscribe to my YouTube channel&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>node</category>
    </item>
    <item>
      <title>React Tutorial: Change State with React Hooks and Mouse Events</title>
      <dc:creator>Hunter Becton</dc:creator>
      <pubDate>Wed, 06 Nov 2019 13:35:29 +0000</pubDate>
      <link>https://dev.to/hunterbecton/react-tutorial-change-state-with-react-hooks-and-mouse-events-1g3m</link>
      <guid>https://dev.to/hunterbecton/react-tutorial-change-state-with-react-hooks-and-mouse-events-1g3m</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I came across a really cool and useful hover effect on &lt;a href="https://colorsandfonts.com/fonts.html" rel="noopener noreferrer"&gt;Colors &amp;amp; Fonts&lt;/a&gt; that allows you to compare font pairings with different colors:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frvvwgcygwghmetg8sfio.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frvvwgcygwghmetg8sfio.gif" alt="Font Pairing Contrast Comparison from Colors and Fonts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This interaction inspired me to tackle my own version in React and share it with the community. In this tutorial you will use the &lt;code&gt;useState&lt;/code&gt; React Hook and Emotion to create the following:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/gvqfn"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here's a 9-minute, step-by-step video below that you can watch if you'd prefer to follow along that way. If you enjoy it, &lt;a href="https://www.youtube.com/channel/UCvHKiUI75ytqUcN851fRR2w?sub_confirmation=1" rel="noopener noreferrer"&gt;be sure to subscribe&lt;/a&gt;! Otherwise, each step is outlined below the video. Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SzYnG9mVDF4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Skip Building the Card Component
&lt;/h3&gt;

&lt;p&gt;The main focus of this tutorial is about &lt;em&gt;how to change state with React Hooks and mouse events&lt;/em&gt;, so to save you some time I went ahead and created a &lt;a href="https://codesandbox.io/s/change-state-onmouseenter-onmouseout-before-hvweh" rel="noopener noreferrer"&gt;Codesandbox project that you can fork&lt;/a&gt; with the basic Card component completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Talk About Emotion(s)
&lt;/h3&gt;

&lt;p&gt;In this tutorial we'll be using &lt;a href="https://emotion.sh/docs/introduction" rel="noopener noreferrer"&gt;Emotion&lt;/a&gt;, a CSS in JS framework. If you've never heard of a CSS in JS framework, there's a couple of &lt;a href="https://mxstbr.com/thoughts/css-in-js/" rel="noopener noreferrer"&gt;key benefits&lt;/a&gt;, including: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add, change and delete CSS without any unexpected consequences and avoid dead code.&lt;/li&gt;
&lt;li&gt;Never go on a hunt for CSS affecting your components ever again.&lt;/li&gt;
&lt;li&gt;Avoid common CSS frustrations to keep a neat codebase and moving quickly, regardless of experience levels.&lt;/li&gt;
&lt;li&gt;Send only the critical CSS to the user for a rapid first paint.&lt;/li&gt;
&lt;li&gt;Simply style your components with a global theme or based on different states.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last benefit in the above list is what we're going to focus on today because we're going to use the state from our &lt;code&gt;useState&lt;/code&gt; React Hooks to dynamically change the colors of the fonts and background. &lt;/p&gt;

&lt;p&gt;So that's the intro, let's get to coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  Create State Using the &lt;code&gt;useState&lt;/code&gt; React Hook
&lt;/h2&gt;

&lt;p&gt;We first need to import &lt;code&gt;useState&lt;/code&gt; from React at the top of the Card component. After it's imported we'll use array destructuring to create the state variable and the function to update the state. You can name these two values anything you want, but a good practice is to give your state variable a descriptive name like &lt;code&gt;background&lt;/code&gt; and follow the function naming convention with &lt;code&gt;set&lt;/code&gt; + &lt;code&gt;variable name&lt;/code&gt;–so &lt;code&gt;setBackground&lt;/code&gt; in this case.&lt;/p&gt;

&lt;p&gt;Next we declare &lt;code&gt;useState()&lt;/code&gt;, passing a default value in parenthesis. The default value for &lt;code&gt;background&lt;/code&gt; will be a hex code for the background color, which is an off-white value of &lt;code&gt;#fdfdfd&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We also need to create a state value and function for the font color, so create another instance of &lt;code&gt;useState&lt;/code&gt; with a state variable of &lt;code&gt;font&lt;/code&gt; and a function of &lt;code&gt;setFont&lt;/code&gt;. The default value for this state variable is the following hex code: &lt;code&gt;#424246&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now with state values initiated in our component we can pass them into the CSS, replacing the hex code values with the default state values. We're not covering much of Emotion in this tutorial, but do take note that I'm storing the CSS in constants and passing the constants into the &lt;code&gt;className&lt;/code&gt; below. You could pass the CSS directly into the &lt;code&gt;className&lt;/code&gt;, but breaking them into constants makes it a little easier to grok.&lt;/p&gt;

&lt;p&gt;By now the Card component should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note in the above code that I used &lt;code&gt;div&lt;/code&gt; tags for the 3 buttons instead of &lt;code&gt;button&lt;/code&gt; tags like I did in the video.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Method to Handle the State Functions
&lt;/h2&gt;

&lt;p&gt;Right below where we declared the state values and functions, create a new arrow function called &lt;code&gt;setStyle&lt;/code&gt; that takes two parameters: &lt;code&gt;background&lt;/code&gt; and &lt;code&gt;font&lt;/code&gt;. These parameters will be hex code string values that we will pass into our state functions as arguments in order to update the state.&lt;/p&gt;

&lt;p&gt;In order to update the state we need to call the two state functions, passing the &lt;code&gt;background&lt;/code&gt; parameter into the &lt;code&gt;setBackground&lt;/code&gt; function and the &lt;code&gt;font&lt;/code&gt; parameter into the &lt;code&gt;setFont&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Your new &lt;code&gt;setStyle&lt;/code&gt; method should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Add &lt;code&gt;onMouseEnter&lt;/code&gt; and &lt;code&gt;onMouseOut&lt;/code&gt; Events to the Buttons
&lt;/h2&gt;

&lt;p&gt;Let's bring this all together by writing &lt;code&gt;onMouseEnter&lt;/code&gt; and &lt;code&gt;onMouseOut&lt;/code&gt; events in each button, passing in the hex code values we want to set. Also note that it's important to write these as arrow functions or the code will execute when the component in rendered.&lt;/p&gt;

&lt;p&gt;And that's it! Now your Card component should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Continue Learning
&lt;/h2&gt;

&lt;p&gt;If you made it this far, thank you! You can keep up with all my content by &lt;a href="https://www.youtube.com/channel/UCvHKiUI75ytqUcN851fRR2w?sub_confirmation=1" rel="noopener noreferrer"&gt;subscribing to the Skillthrive Youtube channel&lt;/a&gt;. I have a number of courses on there that you can start watching for free, including a 3.5 hour course on &lt;a href="https://youtu.be/4Wh5mTyn5WI" rel="noopener noreferrer"&gt;how to build a blog from scratch&lt;/a&gt; using React, Gatsby, and Contentful.&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Would you buy a design system with React components?</title>
      <dc:creator>Hunter Becton</dc:creator>
      <pubDate>Sun, 20 Oct 2019 23:22:26 +0000</pubDate>
      <link>https://dev.to/hunterbecton/would-you-buy-a-design-system-with-react-components-ef6</link>
      <guid>https://dev.to/hunterbecton/would-you-buy-a-design-system-with-react-components-ef6</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yBaKGdaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uh1ezlrva5wcjhfyjwi2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yBaKGdaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uh1ezlrva5wcjhfyjwi2.png" alt="Some components currently being designed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm putting together a design system with 100+ (and growing) components in Figma and React. Considering it's well designed and coded, would designers and developers buy it?&lt;/p&gt;

&lt;p&gt;If so, what would you need to see to help you make the purchase?&lt;/p&gt;

&lt;p&gt;If not, why?&lt;/p&gt;

&lt;p&gt;Thank you for your input.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Code a Skewed Card Design Using CSS 2D Transforms</title>
      <dc:creator>Hunter Becton</dc:creator>
      <pubDate>Sun, 11 Aug 2019 18:00:41 +0000</pubDate>
      <link>https://dev.to/hunterbecton/code-a-skewed-card-design-using-css-2d-transforms-28in</link>
      <guid>https://dev.to/hunterbecton/code-a-skewed-card-design-using-css-2d-transforms-28in</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/css/css3_2dtransforms.asp" rel="noopener noreferrer"&gt;CSS 2D transforms&lt;/a&gt; allow you to modify an element by its shape, size, and position. There are currently 9 methods you can use with the &lt;code&gt;transform&lt;/code&gt; property:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;translate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rotate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;scaleX()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;scaleY()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;scale()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;skewX()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;skewY()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;skew()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;matrix()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today we'll be using &lt;code&gt;skewX()&lt;/code&gt;, &lt;code&gt;skewY()&lt;/code&gt;, &lt;code&gt;scale()&lt;/code&gt;, and &lt;code&gt;rotate()&lt;/code&gt; to create the following card design:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/xt4qe"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: you can also use &lt;code&gt;skew(x,y)&lt;/code&gt; as a shorthand for &lt;code&gt;skewX()&lt;/code&gt; and &lt;code&gt;skewY()&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a step-by-step video below that you can watch if you'd prefer to follow along that way. Otherwise, each step is outlined below the video. Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/GHpq3gJYtmU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Development Environment
&lt;/h3&gt;

&lt;p&gt;We're going to be using &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt; as the code editor with the &lt;a href="https://marketplace.visualstudio.com/items?itemName=sidthesloth.html5-boilerplate" rel="noopener noreferrer"&gt;HTML5 Boilerplate&lt;/a&gt; and &lt;a href="https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer" rel="noopener noreferrer"&gt;Live Server&lt;/a&gt; extensions installed. Also follow the quick documentation here to &lt;a href="https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line" rel="noopener noreferrer"&gt;launch VS Code projects from the command line using a Mac&lt;/a&gt; in case you don't have that setup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the Project
&lt;/h3&gt;

&lt;p&gt;Let's use the VS Code command line to create a project. Open VS Code and select &lt;em&gt;Terminal &amp;gt; New Terminal&lt;/em&gt;. A terminal window will open at the bottom of VS Code. If you're new or need a refresher on terminal commands check out this &lt;a href="https://gist.github.com/poopsplat/7195274" rel="noopener noreferrer"&gt;Mac Terminal Cheat Sheet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Head to the directory you'd like to create the project in using the &lt;code&gt;cd&lt;/code&gt; (change directory) command followed by the directory's location. You can get an idea of what's available by typing &lt;code&gt;ls&lt;/code&gt; (list) in the command line first. I will head to the Desktop by typing &lt;code&gt;cd Desktop/&lt;/code&gt; and pressing return. If you need to go back a directory you can type &lt;code&gt;cd ..&lt;/code&gt; in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: you can begin typing and then press Tab to autocomplete.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll start creating the project by using the &lt;code&gt;mkdir&lt;/code&gt; (make directory) command followed by the name of our project. We'll do &lt;code&gt;mkdir slanted-card&lt;/code&gt;. Once that directory is made we need to go into it by typing &lt;code&gt;cd slanted-card/&lt;/code&gt; and pressing return.&lt;/p&gt;

&lt;p&gt;Inside the slanted-card directory we will create another directory for the &lt;code&gt;images&lt;/code&gt; by typing &lt;code&gt;mkdir images&lt;/code&gt; and pressing return. Then we'll create two files using the &lt;code&gt;touch&lt;/code&gt; command: &lt;code&gt;touch index.html styles.css&lt;/code&gt;. Now type &lt;code&gt;ls&lt;/code&gt; in the command line to list everything in the slanted-card directory. You should see &lt;code&gt;images index.html styles.css&lt;/code&gt; printed.&lt;/p&gt;

&lt;p&gt;Next, type &lt;code&gt;code .&lt;/code&gt; to open the project in a new VS Code window. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: you'll need to setup this &lt;code&gt;code&lt;/code&gt; shortcut on a Mac using the &lt;a href="https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line" rel="noopener noreferrer"&gt;launch projects from VS Code documentation&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The last thing we need to do before we start writing the HTML is to import the logo into the &lt;code&gt;images&lt;/code&gt; folder. You can &lt;a href="https://www.dropbox.com/sh/ne6w7rpelkdbdoy/AAAKpXLM0DsLBq1IeORE_83-a?dl=0" rel="noopener noreferrer"&gt;download the logo I used here&lt;/a&gt; (3 more variations provided), but feel free to use your own. I recommend using SVGs of 20-25 pixels in height.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the HTML
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;index.html&lt;/code&gt; file in VS Code and start typing &lt;code&gt;html&lt;/code&gt; and &lt;code&gt;html-boilerplate&lt;/code&gt; should appear in the dropdown menu. Select this and press return and the HTML5 Boilerplate extension will insert boilerplate code to start your project. Feel free to fill out the title and description metadata, but it's not necessary for the design. However, it is important to import the &lt;code&gt;styles.css&lt;/code&gt; file in the &lt;code&gt;&amp;lt;link rel="stylesheet" href="styles.css"&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Get rid of everything between the opening and closing &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags that was inserted from the boilerplate extension. Inside the body tags write the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There's not much going on here with the HTML, but I do want to point out that we're setting the source to the &lt;code&gt;.customer__picture img&lt;/code&gt; to a direct Unsplash image URL.&lt;/p&gt;

&lt;p&gt;In order to get the direct URL first head to &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; and find an image you want to use. Then right-click on the image and select &lt;em&gt;Open Image in New Tab&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;At the top of the browser towards the end of the URL you will see a string that starts with &lt;code&gt;w=&lt;/code&gt; followed by a number. You can change this number to the image width you want and press return to get the new image. Copy the entire URL and use it as the &lt;code&gt;src&lt;/code&gt; for the &lt;code&gt;.customer__picture img&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's get the project launched via Live Server. Right-click on the &lt;code&gt;index.html&lt;/code&gt; file in the VS Code Explorer and select &lt;em&gt;Open with Live Server&lt;/em&gt;. A new browser window will open and you'll see the HTML you just wrote! 🤘&lt;/p&gt;

&lt;h2&gt;
  
  
  Write the CSS
&lt;/h2&gt;

&lt;p&gt;In the beginning of this section we'll focus on the CSS for the general card design. At the end we'll create the skewed card effect using CSS 2D transforms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Import Google Fonts
&lt;/h3&gt;

&lt;p&gt;Head here to grab the &lt;a href="https://fonts.google.com/specimen/Lato" rel="noopener noreferrer"&gt;Lato typeface from Google Fonts&lt;/a&gt;. In order to gain access to the Lato font we need to &lt;code&gt;@import&lt;/code&gt; it at the top of the &lt;code&gt;styles.css&lt;/code&gt; file. Here's how to get the code you need from Google Fonts:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/hcYjmWJ-J5A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now just paste that code at the top of your CSS file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: be sure to only import the fonts and weights you need. The &lt;em&gt;Load Time&lt;/em&gt; metric helps ensure your project isn't importing too many fonts.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Resets and Body Styles
&lt;/h3&gt;

&lt;p&gt;We'll start by adding simple &lt;a href="https://css-tricks.com/reboot-resets-reasoning/" rel="noopener noreferrer"&gt;CSS resets&lt;/a&gt; and body styles:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note: the body styles are only necessary to center the card in the browser.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Card Styles
&lt;/h3&gt;

&lt;p&gt;Let's add the styles for the basic card design:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Your design should now look like this:&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%2Fi.imgur.com%2FWUhScLo.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%2Fi.imgur.com%2FWUhScLo.png" alt="Simple UI Card Design"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add CSS Transform Styles
&lt;/h3&gt;

&lt;p&gt;We'll first add a &lt;code&gt;skewX()&lt;/code&gt; and &lt;code&gt;skewY()&lt;/code&gt; transform to the &lt;code&gt;.shape&lt;/code&gt; style:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(-5deg) skewY(-5deg);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: you can also you the shorthand &lt;code&gt;skew(x,y)&lt;/code&gt;&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%2Fi.imgur.com%2FK8uAIcF.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%2Fi.imgur.com%2FK8uAIcF.png" alt="Skewed Card Design Part 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the entire card will be skewed, including the image, logo, and text. In order to reverse the skew effect on these elements we will add a transform to them as well, but we'll add the opposite value. For example, if we set the skew to -5 on the card we will set a skew of 5 on each element we want to revert.&lt;/p&gt;

&lt;p&gt;Let's start by adding a transform skew to the &lt;code&gt;.customer__picture img&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(5deg) skewY(5deg);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You'll notice now that the edge of the image is now visible. To fix this we'll add the scale method to the image and scale it by 15%:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(5deg) skewY(5deg) scale(1.15)&lt;/code&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%2Fi.imgur.com%2FPECuF5z.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%2Fi.imgur.com%2FPECuF5z.png" alt="Skewed Card Design Part 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next element we need to fix is the &lt;code&gt;.text&lt;/code&gt; class. So, we'll add the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(5deg) skewY(5deg);&lt;/code&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%2Fi.imgur.com%2FMCspDO6.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%2Fi.imgur.com%2FMCspDO6.png" alt="Skewed Card Design Part 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last thing we need to use transform on is the shape shadow. We want the shadow to be skewed by the same amount as the &lt;code&gt;.shape&lt;/code&gt; class, but currently the skew on the &lt;code&gt;.shape&lt;/code&gt; class is not affecting the shadow because the shadow is not a child of the &lt;code&gt;.shape&lt;/code&gt; class. &lt;/p&gt;

&lt;p&gt;To fix this we'll add the following to the &lt;code&gt;.shape__shadow&lt;/code&gt; class:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(-5deg) skewY(-5deg);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the shadow will be invisible because it's behind the shape. So we'll add a rotate method to the transform in order to make the shadow rotated and visible:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transform: skewX(-5deg) skewY(-5deg) rotate(-20deg);&lt;/code&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%2Fi.imgur.com%2FijRSmeF.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%2Fi.imgur.com%2FijRSmeF.png" alt="Skewed Card Design Part 4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! Now your final CSS should like like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Continue Learning
&lt;/h2&gt;

&lt;p&gt;If you made it this far, thank you! You can keep up with all my content on by &lt;a href="https://www.youtube.com/channel/UCvHKiUI75ytqUcN851fRR2w?sub_confirmation=1" rel="noopener noreferrer"&gt;subscribing to the Skillthrive Youtube channel&lt;/a&gt;. I have a number of courses on there that you can start watching for free, including a 3.5 hour course on &lt;a href="https://youtu.be/4Wh5mTyn5WI" rel="noopener noreferrer"&gt;how to build a blog from scratch&lt;/a&gt; using React, Gatsby, and Contentful.&lt;/p&gt;

</description>
      <category>css</category>
      <category>beginners</category>
      <category>design</category>
    </item>
    <item>
      <title>Build a React Accordion Component Using React Hooks</title>
      <dc:creator>Hunter Becton</dc:creator>
      <pubDate>Mon, 15 Jul 2019 20:42:32 +0000</pubDate>
      <link>https://dev.to/hunterbecton/build-a-react-accordion-component-using-react-hooks-jg2</link>
      <guid>https://dev.to/hunterbecton/build-a-react-accordion-component-using-react-hooks-jg2</guid>
      <description>&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%2Fcdn-images-1.medium.com%2Fmax%2F3840%2F1%2A1DNWUA_ztdBffw3job4A4g.jpeg" 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%2Fcdn-images-1.medium.com%2Fmax%2F3840%2F1%2A1DNWUA_ztdBffw3job4A4g.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Build a React Accordion Component from Scratch Using React Hooks
&lt;/h2&gt;

&lt;p&gt;In this tutorial you will learn how to create an animated React accordion component using React Hooks like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useRef&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are React Hooks?
&lt;/h2&gt;

&lt;p&gt;In simplest form, React Hooks make it possible to use state without the need to create a class. If that’s enough to sell you on using Hooks, then read (or watch) on. However, if you’d like a deeper dive into React Hooks check out &lt;a href="https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889" rel="noopener noreferrer"&gt;Making Sense of React Hooks&lt;/a&gt; by &lt;a href="https://medium.com/@dan_abramov" rel="noopener noreferrer"&gt;Dan Abramov&lt;/a&gt;, co-author of Redux and Create React App.&lt;/p&gt;

&lt;p&gt;You can get the final code &lt;a href="https://codesandbox.io/s/react-accordion-using-react-hooks-9uuvc" rel="noopener noreferrer"&gt;here&lt;/a&gt; from CodeSandbox.io. I’ve also recorded the entire process in the video below and outlined each step in the subsequent post–enjoy!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/MAD2HnUFjgg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Project
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/9uuvc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a React App in CodeSandbox.io
&lt;/h2&gt;

&lt;p&gt;To get started head to &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox.io&lt;/a&gt; and sign up with your &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; account. CodeSandbox is a web-based code editor that makes it easy to write code and create web applications without the need to setup anything locally on your computer–perfect for a quick tutorial.&lt;/p&gt;

&lt;p&gt;Once you’re on the dashboard click on the Create Sandbox button and select the React Client Template. This will create a basic React application that you can start using to build the accordion.&lt;/p&gt;

&lt;p&gt;Before you start writing the accordion component the first thing you want to do is remove the out-of-the-box styling in the &lt;code&gt;styles.css&lt;/code&gt; file. Then add the following global styles:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Setting &lt;code&gt;box-sizing: border-box&lt;/code&gt; ensures that padding is added to the inside of elements while &lt;code&gt;margin: 0&lt;/code&gt; and &lt;code&gt;padding: 0&lt;/code&gt; ensures the browser isn’t adding any default padding to elements.&lt;/p&gt;

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

&lt;p&gt;After cleaning up some of the boilerplate code go ahead and create a new folder in your project called &lt;code&gt;Components&lt;/code&gt;. Inside this folder create two files: &lt;code&gt;Accordion.js&lt;/code&gt; and &lt;code&gt;Accordion.css&lt;/code&gt;. You’ll start with the most basic version of the accordion component, only passing in props and not using React Hooks. You’ll add React Hooks later in the tutorial. With that said, open the &lt;code&gt;Accordion.js&lt;/code&gt; file and add the following:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this code you created a simple function (stateless) component, passing in props. One prop you used is &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt;. It’s important to note that improper use of &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; can open you up to a &lt;a href="https://en.wikipedia.org/wiki/Cross-site_scripting" rel="noopener noreferrer"&gt;cross-site scripting (XSS)&lt;/a&gt; attack. Before using dangerouslySetInnerHTML in your project be sure to read &lt;a href="https://dev.to/jam3/how-to-prevent-xss-attacks-when-using-dangerouslysetinnerhtml-in-react-1464"&gt;How to prevent XSS attacks when using dangerouslySetInnerHTML in React&lt;/a&gt; by Jacob Jang.&lt;/p&gt;

&lt;p&gt;With the most basic form of the accordion component built you can now import it into your &lt;code&gt;index.js&lt;/code&gt; file and use it in the return, passing in props for &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;content&lt;/code&gt; like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;For the demo I used &lt;a href="https://loremipsum.io/" rel="noopener noreferrer"&gt;LoremIpsum.io&lt;/a&gt; to generate filler text. It’s also important to note that in the last accordion I wrote HTML within the content prop in order to show you how &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; will render HTML within a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create the Accordion CSS Styles
&lt;/h2&gt;

&lt;p&gt;Now it’s time to style the accordion component by writing the following in the &lt;code&gt;Accordion.css&lt;/code&gt; file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I’ll spare too much detail here and refer to the comments I left in the CSS file to explain what the classes are doing. However, I do think it’s important to point out that you’ll be passing in some styles like &lt;code&gt;.active&lt;/code&gt; and &lt;code&gt;.rotate&lt;/code&gt; to React state later in the tutorial. Also, the &lt;code&gt;transition&lt;/code&gt; property is being used on classes like &lt;code&gt;accordion__icon&lt;/code&gt; to create &lt;a href="https://www.w3schools.com/css/css3_transitions.asp" rel="noopener noreferrer"&gt;smooth animated transitions&lt;/a&gt; when a CSS property changes or when new classes are added to elements.&lt;/p&gt;

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

&lt;p&gt;Each accordion will have a chevron on the right side that will rotate to point downwards when active. To create the chevron you’ll start by going to &lt;a href="https://fontawesome.com/" rel="noopener noreferrer"&gt;FontAwesome.com&lt;/a&gt; and downloading the SVG for the &lt;a href="https://fontawesome.com/icons/chevron-right?style=solid" rel="noopener noreferrer"&gt;chevron-right icon&lt;/a&gt;. Here’s a quick GIF on how to do that:&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A9pXF7bc3omGIkDhmMPtLyQ.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A9pXF7bc3omGIkDhmMPtLyQ.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the SVG finishes downloading open it in a text/code editor of choice. Then copy everything in the file and head back to CodeSandbox. Once you’re there create a new file in the &lt;code&gt;Components&lt;/code&gt; folder called &lt;code&gt;Chevron.js&lt;/code&gt; and write a simple function component, passing in what you copied from the SVG in the return like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice the props this component is using: &lt;code&gt;className&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt;, &lt;code&gt;width&lt;/code&gt;, and &lt;code&gt;fill&lt;/code&gt;. You’ll use those in the &lt;code&gt;Accordion.js&lt;/code&gt; file like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Add React Hooks to the Accordion Component
&lt;/h2&gt;

&lt;p&gt;Now let’s get to the real reason you’re here: &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Hooks&lt;/a&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  Implement useState: setActive and setActiveState
&lt;/h3&gt;

&lt;p&gt;The first hook you’ll use is &lt;code&gt;useState()&lt;/code&gt;, which you’ll add to your Accordion component. With hooks there’s no need to convert a function component into a class component–just initialize the hook like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;First, you’ll need to import &lt;code&gt;useState&lt;/code&gt; from React. Then you’ll use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring" rel="noopener noreferrer"&gt;array destructuring syntax&lt;/a&gt; to set the current state value (&lt;code&gt;setActive&lt;/code&gt;) and function that allows you to update the state (&lt;code&gt;setActiveState&lt;/code&gt;). This new constant is set equal to &lt;code&gt;useState&lt;/code&gt; and the only argument &lt;code&gt;useState&lt;/code&gt; needs is the initial value, which in this case is an empty string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the toggleAccordion Function
&lt;/h3&gt;

&lt;p&gt;Now that you have your first state defined you need to create a function within the component that will be called when the accordion is clicked by an user. To make sure it’s clear what the function does, you can name it &lt;code&gt;toggleAccordion&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This first thing this function will do is call &lt;code&gt;setActiveState()&lt;/code&gt;. Within &lt;code&gt;setActiveState()&lt;/code&gt; you’ll write the following &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator" rel="noopener noreferrer"&gt;ternary operator&lt;/a&gt;: &lt;code&gt;setActiveState(setActive === “” ? “active" : “”&lt;/code&gt;. This ternary operator is checking if &lt;code&gt;setActive&lt;/code&gt; is empty, and if it is it will change the state to active. If it is already set to &lt;code&gt;active&lt;/code&gt; it will set it to an empty string.&lt;/p&gt;

&lt;p&gt;You can then use the value of &lt;code&gt;setActive&lt;/code&gt; within the button &lt;code&gt;className&lt;/code&gt; using a template string. Last, pass an &lt;code&gt;onClick&lt;/code&gt; event listener that calls the new &lt;code&gt;toggleAccordion&lt;/code&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now when you save your project you can click on the accordion and the background color will stay the value you set on the &lt;code&gt;.active&lt;/code&gt; class in your &lt;code&gt;Accordion.css&lt;/code&gt; file. You can always inspect the element to see the class toggle from &lt;code&gt;accordion&lt;/code&gt; to &lt;code&gt;accordion active&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implement useRef to Identify scrollHeight
&lt;/h3&gt;

&lt;p&gt;The next React Hook you’ll use in the tutorial is &lt;code&gt;useRef()&lt;/code&gt;. This hook will create a reference to a DOM element, which you can then get data off of by accessing &lt;code&gt;.current&lt;/code&gt;. The data you want to access is &lt;code&gt;scrollHeight&lt;/code&gt;, which gives you the height of an element even when the element’s overflow is hidden.&lt;/p&gt;

&lt;p&gt;You then can use the data from &lt;code&gt;scrollHeight&lt;/code&gt; to change the max-height of the content within the accordion, which you’ll initially set to 0. Doing this will create an animated expand/collapse effect for the accordion content.&lt;/p&gt;

&lt;p&gt;To start you need to import &lt;code&gt;useRef&lt;/code&gt; from React and initialize it within the accordion component. You can call this reference &lt;code&gt;content&lt;/code&gt; so it’s easy to remember what it’s referencing. You’ll also want to set its initial value to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then you can set the reference on the content element by passing in the following HTML attribute: &lt;code&gt;ref={content}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In order to see the value of each reference you can &lt;code&gt;console.log&lt;/code&gt; the value in the &lt;code&gt;toggleAccordion&lt;/code&gt; function. By now you should have the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now you can open the console within CodeSandbox and see the &lt;code&gt;scrollHeight&lt;/code&gt; of each accordion content section printing to the console when you click on it. Feel free to remove the &lt;code&gt;console.log&lt;/code&gt; once you’re done testing it out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implement useState: setHeight and setHeightState
&lt;/h3&gt;

&lt;p&gt;With access to the accordion’s content height you can now use that value to create the animated expand/collapse effect. To start you’ll need to create a new state called &lt;code&gt;setHeight&lt;/code&gt; with &lt;code&gt;useState&lt;/code&gt;–just like you did for &lt;code&gt;setActive&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This time set the initial state to &lt;code&gt;0px&lt;/code&gt; because you’ll be using that value to make sure all the content is hidden when the accordion components are rendered.&lt;/p&gt;

&lt;p&gt;You’ll also change &lt;code&gt;setHeight&lt;/code&gt; using &lt;code&gt;setHeightState&lt;/code&gt;, which you’ll call in the &lt;code&gt;toggleAccordion&lt;/code&gt; function. Just like with &lt;code&gt;setActiveState&lt;/code&gt; you will write a ternary operator, but this time you’re checking if &lt;code&gt;setActive&lt;/code&gt; is equal to &lt;code&gt;active&lt;/code&gt;. If it is the function will change &lt;code&gt;setHeight&lt;/code&gt; to &lt;code&gt;0px&lt;/code&gt;. Else, if it’s already &lt;code&gt;0px&lt;/code&gt; it will change to the value of the accordion’s content &lt;code&gt;scrollHeight&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, you’ll use the value of &lt;code&gt;setHeight&lt;/code&gt; to set the &lt;code&gt;maxHeight&lt;/code&gt; via an inline style. With that your code should appear as follows:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once you save that you can click on each accordion to reveal the content. Feel free to change the speed and acceleration of the animation in the &lt;code&gt;transition&lt;/code&gt; property of the &lt;code&gt;.accordion__content&lt;/code&gt; class in your &lt;code&gt;Accordion.css&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implement useState: setRotate and setRotateState
&lt;/h3&gt;

&lt;p&gt;You’re at the homestretch! The last thing you want to do is animate the chevron to point downwards by rotating it 90 degrees clockwise when the accordion’s &lt;code&gt;setActive&lt;/code&gt; state is active.&lt;/p&gt;

&lt;p&gt;To do this you’ll create a new state called &lt;code&gt;setRotate&lt;/code&gt;. You’ll set the initial value equal to the string &lt;code&gt;accordion__icon&lt;/code&gt; and write a similar ternary operator in the &lt;code&gt;toggleAccordion&lt;/code&gt; function that checks the current value of &lt;code&gt;setActive&lt;/code&gt;. This time if &lt;code&gt;setActive&lt;/code&gt; is equal to &lt;code&gt;active&lt;/code&gt; the function will change the &lt;code&gt;setRotate&lt;/code&gt; value to &lt;code&gt;accordion__icon&lt;/code&gt;. Else, if it’s not active it will change &lt;code&gt;setRotate&lt;/code&gt; to &lt;code&gt;accordion__icon rotate&lt;/code&gt;. You’ll then use the value of &lt;code&gt;setRotate&lt;/code&gt; to dynamically change the &lt;code&gt;className&lt;/code&gt; of the chevron. Your &lt;code&gt;Accordion.js&lt;/code&gt; component should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And that’s it! Now when you click on an accordion the chevron will rotate downwards. Click again and the accordion will revert to its initial state.&lt;/p&gt;

&lt;p&gt;Similar to &lt;code&gt;.accordion__content&lt;/code&gt;, you can tweak the animation by changing the &lt;code&gt;transition&lt;/code&gt; property on the &lt;code&gt;.accordion__icon&lt;/code&gt; class in your &lt;code&gt;Accordion.css&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing Words
&lt;/h2&gt;

&lt;p&gt;I hope this tutorial was a great example of how to use React Hooks. If you enjoyed the tutorial be sure to check out my other courses on the &lt;a href="https://www.youtube.com/channel/UCvHKiUI75ytqUcN851fRR2w" rel="noopener noreferrer"&gt;Skillthrive YouTube channel&lt;/a&gt;. And as always, if you have any questions feel free to drop them below.&lt;/p&gt;

</description>
      <category>react</category>
      <category>video</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
