<?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: katbruce</title>
    <description>The latest articles on DEV Community by katbruce (@katbruce).</description>
    <link>https://dev.to/katbruce</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%2F1049127%2F860ad804-4a0f-424d-a9ec-134a6fac498a.png</url>
      <title>DEV Community: katbruce</title>
      <link>https://dev.to/katbruce</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/katbruce"/>
    <language>en</language>
    <item>
      <title>Routing with React - not v6 compatible (yet)</title>
      <dc:creator>katbruce</dc:creator>
      <pubDate>Tue, 25 Apr 2023 22:34:36 +0000</pubDate>
      <link>https://dev.to/katbruce/routing-with-react-3a4g</link>
      <guid>https://dev.to/katbruce/routing-with-react-3a4g</guid>
      <description>&lt;p&gt;One of the most useful attributes of React is the ability to navigate between multiple pages or views is a fundamental element of any website. Routing in React allows us to create and navigate between different routes in our application adds even more functionality to your React apps. The routing protocol within React's routing library makes the process much more streamlined than it is with vanilla JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Router
&lt;/h3&gt;

&lt;p&gt;React Router is a third-party library that provides routing functionality in React applications. It is the most popular routing library for React and is widely used in production applications. &lt;/p&gt;

&lt;p&gt;This tutorial is assuming that you already have a react app created, if not: create a new app before starting any of the below instructions. &lt;/p&gt;

&lt;h3&gt;
  
  
  File Structures
&lt;/h3&gt;

&lt;p&gt;Assuming there's already a src folder, inside of that create a file for each of the routes you'll need, a header file, and your App.js. &lt;/p&gt;

&lt;h3&gt;
  
  
  Routes
&lt;/h3&gt;

&lt;p&gt;In this example our routes will be Home, About, and Contact. &lt;/p&gt;

&lt;p&gt;A framework for each of those files should follow the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

function Home() {
  return (
    &amp;lt;h1&amp;gt;Home Page&amp;lt;/h1&amp;gt;;
    //anything else you think should go in this component
  );
}
export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;p&gt;Next we focus on a Header file. Create Header.js in the src folder and we will create a navigation bar to make a user-friendly way to navigate between pages in your application. _&lt;/p&gt;

&lt;p&gt;We import Link from "react-router-dom" and React from "react". Then, using an unordered list we identify the relative links to all of the pages we have.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Link } from "react-router-dom";

function Header() {
  return (
    &amp;lt;&amp;gt;****
      &amp;lt;header&amp;gt;
        &amp;lt;ul &amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/"&amp;gt;Home&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/about"&amp;gt;About&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/contact"&amp;gt;Contact&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
export default Header;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  App.js
&lt;/h3&gt;

&lt;p&gt;Then, we move to the App.js file. This is where we use the router to route to each of the pages we linked in the header. &lt;/p&gt;

&lt;p&gt;To define routes in React, we need to import the react-router-dom library's key elements: Switch and Route. Next, we import the Header and each component we are routing to. If there are issues with this part, make sure you exported the functions created in their respective files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Route, Switch } from "react-router-dom";
import Header from './Header'
import Home from './Home';
import About from './About';
import Contact from './Contact';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use what we've imported, we use the code below to get a functioning router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Header /&amp;gt;

      &amp;lt;Switch&amp;gt;
        &amp;lt;Route exact path="/"&amp;gt;
          &amp;lt;Home /&amp;gt;
        &amp;lt;/Route&amp;gt;
        &amp;lt;Route exact path="/about"&amp;gt;
          &amp;lt;About /&amp;gt;
        &amp;lt;/Route&amp;gt;
        &amp;lt;Route exact path="/contact"&amp;gt;
          &amp;lt;Contact /&amp;gt;
        &amp;lt;/Route&amp;gt;
      &amp;lt;/Switch&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Index.js
&lt;/h3&gt;

&lt;p&gt;Then, update your index.js to use BrowserRouter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactDOM from "react-dom";
import { BrowserRouter as Router } from "react-router-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  &amp;lt;Router&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/Router&amp;gt;,
  rootElement
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Styling
&lt;/h3&gt;

&lt;p&gt;After this we should have a functioning nav-bar. But to make it even better, we should add some css styling. By adding flexbox, each routing link should be inline with each other, which is objectively how any nav-bar should look. In the css file, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header ul {
  display: flex;
}

header ul li {
  list-style: none;
  padding: 10px;
}

header li a {
  text-decoration: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see the code in Action: check out &lt;a href="https://codesandbox.io/s/routing-in-react-u5h94b?file=/src/Home.js"&gt;the code on Code Sandbox&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Variable Scope in JavaScript: Beginner's Guide</title>
      <dc:creator>katbruce</dc:creator>
      <pubDate>Thu, 06 Apr 2023 20:41:25 +0000</pubDate>
      <link>https://dev.to/katbruce/variable-scope-in-javascript-beginners-guide-246i</link>
      <guid>https://dev.to/katbruce/variable-scope-in-javascript-beginners-guide-246i</guid>
      <description>&lt;p&gt;For any code more complex than console logging "Hello world!", variables are necessary for programming in JavaScript. The scope of a variable is the region in which each variable can be called and used. &lt;/p&gt;

&lt;p&gt;A few factors affect a variable's scope: where it is initially declared and what declaration keyword is used when initializing the variable. &lt;/p&gt;

&lt;h2&gt;
  
  
  Variable Types
&lt;/h2&gt;

&lt;p&gt;The three types of declarations are const, let, and var.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;br&gt;
Const, or constant, variables are permanently set to whatever value they are initially declared with. When a value will not change within the scope of the program (or code block), a const variable is ideal to prevent accidental reassignments. Some common uses for the const variable are for mathematic constants, names (that are unchanging), and for grabbing HTML elements for DOM manipulation (which is what I’ve been using const variables for the most lately). These constant variables can be either block or global scope, depending on where they are declared in your code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;br&gt;
Let variables can be reassigned as many times as needed within the scope. Some examples of where this works well are for iterators, variables taking inputs and summations, along with countless others. Let variables, like const variables, can be either block or global scoped. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;br&gt;
Var is a function-scope variable that is similar to let in that it lets you reassign the variable to any value as many times as needed. The issue with the var keyword is that there is a potential for hoisting, or using the variable before the declaration, which can create bugs related to scope. These can be irritating to debug, so the best practice is to use let or const declarations instead. &lt;/p&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Global&lt;/strong&gt;&lt;br&gt;
A global variable is declared outside any curly brackets. It belongs to no function, loop, or other blocks of code, which means that every code block in your program will have access to that variable. A global variable can be declared with any declaration: var, let, or const. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block&lt;/strong&gt;&lt;br&gt;
A block variable is any variable declared with let or const within a code block. This variable is only accessible within the code block it was declared in. A code block is anything within curly brackets; this could be a function, for loop, if statement, etc. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;br&gt;
A function scope variable that is declared with var. It works similarly to a block scope but is only applicable to functions, not other types of code blocks. Noted above, it is typically best to just use a block scope variable in place of a function scope variable to avoid scope related bugs. &lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const test1 = 1; //constant global variable
let test2 = 2; //not constant global variable
function foo(){
   let test3 = 3; //not constant block scope variable
   const test4 = 4;//constant block scope variable
   let test5 = test1 + 5;//block scope variable using 
                         //globally declared variable
   console.log(test1, test2, test3, test4, test5);
     //returns 1 2 3 4 6, all variables log to console
} 
console.log(test1, test2, test3, test4, test5)
   //returns 1 2 and errors(out of scope) for test3, test4, test5 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above shows how each type of variable is declared and how it performs in global and block scope. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
