<?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: Jessica</title>
    <description>The latest articles on DEV Community by Jessica (@jesspaul).</description>
    <link>https://dev.to/jesspaul</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%2F606765%2Fab2c5752-00e9-4d9e-b7d7-4bcfafb819c4.png</url>
      <title>DEV Community: Jessica</title>
      <link>https://dev.to/jesspaul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jesspaul"/>
    <language>en</language>
    <item>
      <title>Converting My Vanilla JavaScript Project into React</title>
      <dc:creator>Jessica</dc:creator>
      <pubDate>Fri, 14 May 2021 19:36:43 +0000</pubDate>
      <link>https://dev.to/jesspaul/converting-my-vanilla-javascript-project-into-react-4iob</link>
      <guid>https://dev.to/jesspaul/converting-my-vanilla-javascript-project-into-react-4iob</guid>
      <description>&lt;p&gt;When I first learned React, my mind immediately went to the Super Tic Tac Toe game I built three months ago using Vanilla JavaScript. I thought this project was the perfect candidate to refactor into React because of the potential for reusable components and the use of state for the game logic.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Follow along with the code:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/jesspaul/super-tic-tac-toe" rel="noopener noreferrer"&gt;Original Project Repo&lt;/a&gt; | &lt;a href="https://thirsty-bartik-47f936.netlify.app/" rel="noopener noreferrer"&gt;Deployed Original Project&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/jesspaul/react-super-tic-tac-toe" rel="noopener noreferrer"&gt;React Project Repo&lt;/a&gt; | &lt;a href="https://super-ttt.netlify.app/" rel="noopener noreferrer"&gt;Deployed React Project&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Game Rules
&lt;/h1&gt;

&lt;p&gt;If you haven't played Super Tic Tac Toe, it's an advanced version of the original game where there is a tic tac toe grid inside each of the nine squares of the gameboard's exterior grid. Players take turns placing their symbol anywhere in the exterior grid space that corresponds to the interior grid space the previous player selected. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/w4QLaaFAWs7WhZNETM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/w4QLaaFAWs7WhZNETM/giphy.gif" alt="demonstrating how to take turns in the game"&gt;&lt;/a&gt;&lt;/p&gt;
demonstrating how to take turns in the game


&lt;h2&gt;
  
  
  Components
&lt;/h2&gt;

&lt;p&gt;I began by creating basic React components to match the original HTML, which was a lot of copy and pasted &lt;code&gt;div&lt;/code&gt; elements. It was so quick and easy to dynamically render the nine squares of the exterior grid and nine squares of each interior grid using the &lt;code&gt;.map()&lt;/code&gt; method on an array with nine elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [extValues, setExtValues] = useState([null, null, null, null, null, null, null, null, null]);

return (
    &amp;lt;div className="Gameboard"&amp;gt;
        {
            extValues.map((extValue, extIdx) =&amp;gt; (
                    &amp;lt;ExteriorSquare /&amp;gt;
            ))
        }
    &amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  State
&lt;/h2&gt;

&lt;p&gt;I saved these values in state in order to render what is displayed in the square: an 'X', an 'O', or &lt;code&gt;null&lt;/code&gt; to display nothing. Also saved in state is the current player, the exterior grid square the current player can play in, and the game winner. Because these values are saved in state, the components will re-render whenever their value is changed. This replaces the need to manipulate the DOM as I did in the original project. &lt;/p&gt;

&lt;h2&gt;
  
  
  onClick
&lt;/h2&gt;

&lt;p&gt;In addition to learning React, I also leveled up my JavaScript knowledge since I wrote the original project. In the original, I wrote six (6!!!) separate functions to add or remove click event listeners which provide functionality for the player to place their symbol in the space. In the React version, every interior grid space has the &lt;code&gt;onClick&lt;/code&gt; function, but I used ternary logic to determine if there is no winner yet, the space is currently empty, and the exterior grid space is valid (based on where the previous player played). If this expression evaluates to true, the space is clickable, if not, nothing will happen if the player tries to click on an invalid square.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleClick = (index) =&amp;gt; {
    if (!winner &amp;amp;&amp;amp; values[index] === null &amp;amp;&amp;amp; currentSquare.includes(extIdx)) {
         // functionality to handle click event
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Check for Win
&lt;/h2&gt;

&lt;p&gt;I'm a little embarrassed to share the original code I wrote to check for a win because of how repetitive it is! But it got the job done and I couldn't think of a better way to do it at the time. This is just evidence of my growth as a dev. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/8Y1lJFeb5QumDAhcaO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/8Y1lJFeb5QumDAhcaO/giphy.gif" alt="scrolling through scary, repetitive code"&gt;&lt;/a&gt;&lt;a href="https://i.giphy.com/media/D8sPFkUbKomTItk9Dl/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/D8sPFkUbKomTItk9Dl/giphy.gif" alt="scrolling through more scary, repetitive code"&gt;&lt;/a&gt;&lt;/p&gt;
scrolling through scary, repetitive code



&lt;p&gt;To be honest, I didn't come up with the new code, I turned to Google for help. But googling when you don't know the answer or to find a better, more efficient solution is a valid and necessary skill as a developer. The code in the GIFs above and the code block below are doing the same thing - checking for three matching values to determine if a player has three in a row and won the square. The two GIFs show checking for a win in the interior grid and in the exterior grid. The code below can check both from one function. Yay for efficiency!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const winningPositions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
];

const checkWin = (squares) =&amp;gt; {
    for (let i = 0; i &amp;lt; winningPositions.length; i++) {
        const [a, b, c] = winningPositions[i];
        if (squares[a] &amp;amp;&amp;amp; squares[a] === squares[b] &amp;amp;&amp;amp; squares[a] === squares[c]) {
            return squares[a];
        }
    }
    return null;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Styling
&lt;/h2&gt;

&lt;p&gt;The final piece of this refactor project is the styling, which I tried to keep the same between versions. I have since learned about CSS variables and that it's better to use relative sizes instead of sizing with hard coded pixels. In addition to those minor adjustments, I originally coded borders around the interior grids and used DOM manipulation to remove the borders on the 8 squares with outside edges. Because React works with the Virtual DOM, I decided to simplify this approach and just use background colors and grid gaps to display the borders around each square.&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%2Fuploads%2Farticles%2Frfolebsdi4he9dexrgis.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%2Fuploads%2Farticles%2Frfolebsdi4he9dexrgis.png" alt="Refactored CSS in JS to CSS"&gt;&lt;/a&gt;&lt;/p&gt;
comparing CSS in JS to just CSS



&lt;h2&gt;
  
  
  That's All... For Now!
&lt;/h2&gt;

&lt;p&gt;Thank you for reading about this refactor! If you're just starting to learn development, I would advise you to build projects however you can with your existing knowledge and you can always level up and refactor in the future. Who knows what kind of improvements I'll be able to make in three months!&lt;/p&gt;

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