<?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: Linda Okorie</title>
    <description>The latest articles on DEV Community by Linda Okorie (@dauntless).</description>
    <link>https://dev.to/dauntless</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%2F340465%2F7cb82ab4-1f70-499a-9537-d1a08cb661fa.jpg</url>
      <title>DEV Community: Linda Okorie</title>
      <link>https://dev.to/dauntless</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dauntless"/>
    <language>en</language>
    <item>
      <title>React Router's useRoutes hook</title>
      <dc:creator>Linda Okorie</dc:creator>
      <pubDate>Thu, 13 Jan 2022 16:23:11 +0000</pubDate>
      <link>https://dev.to/dauntless/react-routers-useroutes-hook-38fc</link>
      <guid>https://dev.to/dauntless/react-routers-useroutes-hook-38fc</guid>
      <description>&lt;p&gt;When creating multi-paged apps with React or any other library/framework, a package to handle routing is always used. Whether it is Vue Router for Vue apps, React Router for React apps, etc. Today I'm going to emphasize implementing routes in React using React Router's useRoutes hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Although I will try my best to make this easy enough for a beginner to understand, I advise that before going further you at least have basic knowledge of React and have at least seen React Router used in an application. If you have no experience with React Router, you could look at the documentation first. They give really clear examples that will get you up to speed in no time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach 1: Using the &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; JSX components
&lt;/h2&gt;

&lt;p&gt;This is the primary way of rendering something using React Router and the approach that would be seen used in many places. Hence, I won't dwell much on the syntax of this but drop the example which will be used in the rest of this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;MainLayout.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is a layout component for any page that is not an authentication page.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { Link, Outlet } from "react-router-dom";
export default function MainLayout() {
    return (
        &amp;lt;&amp;gt;
            &amp;lt;nav className="nav-bar"&amp;gt;
                 &amp;lt;ul&amp;gt;
                     &amp;lt;li&amp;gt;
                         &amp;lt;Link to="home"&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="/"&amp;gt; Log In&amp;lt;/Link&amp;gt;
                     &amp;lt;/li&amp;gt;
                     &amp;lt;li&amp;gt;
                         &amp;lt;Link to="signup"&amp;gt; Sign Up&amp;lt;/Link&amp;gt;
                     &amp;lt;/li&amp;gt;
                 &amp;lt;/ul&amp;gt;
            &amp;lt;/nav&amp;gt;
            &amp;lt;Outlet /&amp;gt;
        &amp;lt;/&amp;gt;
    );
}


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;Home.js&lt;/code&gt;
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export default function Home() {
    return (
        &amp;lt;div className="App"&amp;gt;
            &amp;lt;h1&amp;gt;Home Page&amp;lt;/h1&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}


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

&lt;/div&gt;

&lt;p&gt;The other pages used in this example were defined in a similar way as &lt;strong&gt;&lt;code&gt;Home.js&lt;/code&gt;&lt;/strong&gt;. You can look at the sandbox to see them for clarity. These however form the main elements of the example.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import "./styles.css";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from './pages/Home'
import About from './pages/About'
import Login from './pages/Login'
import SignUp from './pages/Signup'
import AuthLayout from './layouts/AuthLayout'
import MainLayout from './layouts/MainLayout'

export default function App() {
    return (
        &amp;lt;BrowserRouter&amp;gt;
            &amp;lt;div className="App"&amp;gt;
                &amp;lt;h1&amp;gt;React Router Example&amp;lt;/h1&amp;gt;
                &amp;lt;Routes&amp;gt;
                    &amp;lt;Route element={&amp;lt;AuthLayout/&amp;gt;}&amp;gt;
                        &amp;lt;Route path="/" element={&amp;lt;Login/&amp;gt;}/&amp;gt;
                        &amp;lt;Route path="signup" element={&amp;lt;SignUp /&amp;gt;}/&amp;gt;
                    &amp;lt;/Route&amp;gt;
                    &amp;lt;Route element={&amp;lt;MainLayout/&amp;gt;}&amp;gt;
                        &amp;lt;Route path="home" element={&amp;lt;Home/&amp;gt;}/&amp;gt;
                        &amp;lt;Route path="about" element={&amp;lt;About /&amp;gt;}/
                    &amp;lt;/Route&amp;gt;
                &amp;lt;/Routes&amp;gt;
             &amp;lt;/div&amp;gt;
        &amp;lt;/BrowserRouter&amp;gt;
     );
}


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

&lt;/div&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/optimistic-bhabha-vmdk2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can read more about this in React Router documentation here. The &lt;strong&gt;&lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt;&lt;/strong&gt; component acts as a wrapper for all possible routes that can be matched. The &lt;strong&gt;&lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;&lt;/strong&gt;  carries an element in its element attribute. If the content of its path attribute matches the current URL, it renders the content of the element attribute. As location changes a user navigates the app, the corresponding elements are rendered.&lt;br&gt;
To make this example as comprehensive as possible, I took the routing from a side project I am working on and it has one kind of route which won't be in a basic example of routes - Layout Routes. A layout route is a parent route without a path attribute used to group child routes under a specific layout. For more on this and other major concepts of React Router have a look at this part of the documentation. It's a bit lengthy but it will get you up-to-speed with the lingo quickly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Approach 2: Using the useRoutes hook
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;routes.js&lt;/code&gt;
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useRoutes } from "react-router-dom";
import Home from './pages/Home'
import About from './pages/About'
import Login from './pages/Login'
import SignUp from './pages/Signup'
import AuthLayout from './layouts/AuthLayout'
import MainLayout from './layouts/MainLayout'

export default function Router() {
let element = useRoutes([
    {
        element: &amp;lt;AuthLayout /&amp;gt;,
        children: [
           { path: "/", element: &amp;lt;Login /&amp;gt; },
           { path: "signup", element: &amp;lt;SignUp /&amp;gt; },
        ],
    },
    {
        element: &amp;lt;MainLayout /&amp;gt;,
        children: [
            { path: "home", element: &amp;lt;Home /&amp;gt; },
            { path: "about", element: &amp;lt;About /&amp;gt; },
        ],
    },
]);
return element;
}


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;App.js&lt;/code&gt;
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import "./styles.css";
import { BrowserRouter } from "react-router-dom";
import Router from './routes'

export default function App() {
return (
    &amp;lt;BrowserRouter&amp;gt;
        &amp;lt;div className="App"&amp;gt;
            &amp;lt;h1&amp;gt;useRoutes Example&amp;lt;/h1&amp;gt;
            &amp;lt;Router/&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
);
}


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

&lt;/div&gt;
&lt;p&gt;The other components are defined the same way as in the previous approach. You can look at the sandbox below to see it all in one cohesive example.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/thirsty-architecture-jgok5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Note that for clarity and separation of concerns in writing code, I do not declare the object-based config in the same file as to where I want to use the routes. In my project, I created a folder called &lt;strong&gt;&lt;code&gt;routes&lt;/code&gt;&lt;/strong&gt; and a file called &lt;strong&gt;&lt;code&gt;index.js&lt;/code&gt;&lt;/strong&gt; to hold the routes. Another common convention I have seen devs do is to create a file in the &lt;strong&gt;&lt;code&gt;src&lt;/code&gt;&lt;/strong&gt; folder called &lt;strong&gt;&lt;code&gt;routes.js&lt;/code&gt;&lt;/strong&gt; and put it all there.&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%2Fuploads%2Farticles%2F371szz3cg7hn3ggtib3t.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%2F371szz3cg7hn3ggtib3t.png" alt="Routes file in routes folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fni4eusgzcz2vz0v51giw.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%2Fni4eusgzcz2vz0v51giw.png" alt="routes.js file in the  raw `src` endraw  folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's look closer at what is happening in the &lt;code&gt;**routes**&lt;/code&gt; file. The routes are inserted as objects in an array, with each object representing one route. The object has keys and values similar to the attributes and values of the &lt;strong&gt;&lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;&lt;/strong&gt; component. To account for nested routes, the object has an optional third key known as &lt;strong&gt;&lt;code&gt;children&lt;/code&gt;&lt;/strong&gt; that allows you to list all child routes of a specific route. The nesting can go as deep as your application requires it to be. &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%2Fuploads%2Farticles%2Ftzy9sku5yhdpo76pta19.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%2Ftzy9sku5yhdpo76pta19.png" alt="Nested routing in useRoutes hook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The return value of the &lt;code&gt;**useRoutes**&lt;/code&gt; hook is either a valid React element or &lt;code&gt;**null**&lt;/code&gt; if nothing matched. Once you are done creating the routes, you then add them to your app in the same place you would have added the &lt;code&gt;**&amp;lt;Routes&amp;gt;**&lt;/code&gt; and &lt;code&gt;**&amp;lt;Route&amp;gt;**&lt;/code&gt; components which for me is my &lt;code&gt;**App.js**&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78t3p63ycyvqtvi2f5k1.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%2F78t3p63ycyvqtvi2f5k1.png" alt="adding routes from routes.js to App.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;routes.js&lt;/code&gt;&lt;/strong&gt; file is imported in &lt;strong&gt;&lt;code&gt;App.js&lt;/code&gt;&lt;/strong&gt; and added just as you would any other component. It was created in a functional component, after all. Remember that hooks can't be used outside of functions and that is why it was declared inside of one. If I intended to use it in &lt;strong&gt;&lt;code&gt;App.js&lt;/code&gt;&lt;/strong&gt;, I would've declared it inside the function as well.&lt;/p&gt;

&lt;p&gt;There aren't any clear benefits of using one approach of routing over another, it all depends on the preference of the developer or team and the use case. For me, abstracting the route config using the hook made it easier for me to understand and anyone visiting my project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;For a closer look at the code used in this article, you can visit these two sandboxes;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/thirsty-architecture-jgok5?file=/src/App.js:0-289" rel="noopener noreferrer"&gt;useRoutes Example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/optimistic-bhabha-vmdk2." rel="noopener noreferrer"&gt;Routes JSX Example&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>What Data Structures and Algorithms should mean to you</title>
      <dc:creator>Linda Okorie</dc:creator>
      <pubDate>Sat, 04 Apr 2020 23:55:51 +0000</pubDate>
      <link>https://dev.to/dauntless/what-data-structures-and-algorithms-should-mean-to-you-9kn</link>
      <guid>https://dev.to/dauntless/what-data-structures-and-algorithms-should-mean-to-you-9kn</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@punttim?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Tim Gouw&lt;/a&gt; on &lt;a href="https://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pursuing a career in Software Engineering is a lot of work. I get a lot of advice from different angles that it seems quite overwhelming. At different points part of the advice included studying Data Structures and Algorithms. One problem I had though was that I was never satisfied with the reasons I was given to study it (maybe that was why I was always scared of it).As a self taught developer who reads materials as it comes my way and relies on answers from Stack Overflow a lot, the only reason I could conjure up for studying DS &amp;amp; A (Data Structures and Algorithms) was I needed to know them in order to answer questions during interviews. It wasn't till I started reading that I understood how wrong I was. &lt;/p&gt;

&lt;p&gt;Learning Data Structures and Algorithms isn't just about the interview – it goes beyond that. Have you ever wondered &lt;strong&gt;WHY&lt;/strong&gt; companies want you to know these things? Its all about implementation! Data Structures were created to improve the time and space complexity. Algorithms are the steps taken to perform a given task. Now naturally, lesser steps would translate to smaller time. This is important to companies because time spent and space consumed translates to money – money for production, money for maintenance. If the software took too much time for work, it wouldn't be of much use to anyone. If it crashed as result, that would mean bringing more people in to fix it.&lt;/p&gt;

&lt;p&gt;When we solve problems as newbies, we just hack till we find solutions. After doing this for a while, patterns begin to emerge. Now instead of going through the same painful process each time you need to solve a problem, DS &amp;amp; A helps us shorten the time it takes to craft a solution. Imagine immediately searching for a number in an array, I know immediately a binary search would be the fastest way to do it! It also optimizes operations for better efficiency – they take lesser time or lesser space (there are trade offs, through). A solution hacked together might work, but the difference in efficiency when compared to a solution carefully crafted using Data Structures is clear.&lt;/p&gt;

&lt;p&gt;Bottom line, having knowledge of Data Structures and Algorithms is like a superpower. Its like the ultimate fashion trend that never goes out of style. You'll be able to implement it no matter the time or the language. Most books on the topic write solutions in pseudo code making it easier to understand despite your background. It also makes you see problems differently. Solving problems won't be about hacking things together anymore. There's a mathematical side to it all (the side that leaves me bewildered the most) but I suggest just trying to understand the basics first and ease into it slowly.&lt;/p&gt;

&lt;p&gt;I'm still studying but would love learn more things that can be gotten out of DS &amp;amp; A.👋&lt;/p&gt;

</description>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
