<?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: Mehrab-18</title>
    <description>The latest articles on DEV Community by Mehrab-18 (@mehrab18).</description>
    <link>https://dev.to/mehrab18</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%2F1009300%2Fd6b99fdd-7516-4c0d-83a0-d2b41d972c36.png</url>
      <title>DEV Community: Mehrab-18</title>
      <link>https://dev.to/mehrab18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehrab18"/>
    <language>en</language>
    <item>
      <title>05 JavaScript One-Liners for every Project</title>
      <dc:creator>Mehrab-18</dc:creator>
      <pubDate>Mon, 13 Mar 2023 07:36:56 +0000</pubDate>
      <link>https://dev.to/mehrab18/05-javascript-one-liners-for-every-project-4kg2</link>
      <guid>https://dev.to/mehrab18/05-javascript-one-liners-for-every-project-4kg2</guid>
      <description>&lt;h2&gt;
  
  
  JavaScript is a powerful language that can accomplish a lot with minimal code.
&lt;/h2&gt;

&lt;p&gt;The code snippets known as one-liners exemplify this, as they often perform complex tasks in just a single line. In this article, we'll explore 10 essential one-liners that you should use in almost every JavaScript project.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Capitalize Text&lt;/strong&gt;
Capitalizing strings is not a built-in feature of JavaScript, but it's a task we often need to perform. To capitalize a string, we can create a capitalize function that accepts a string and makes the first letter uppercase, then appends the rest of the string.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--37DsMJTm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zllpyalvgms3rb492z5k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--37DsMJTm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zllpyalvgms3rb492z5k.png" alt="Image description" width="802" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Get a Random Element&lt;/strong&gt;
Getting a random element from an array is helpful when you want to create unique experiences for users. For instance, you might want to display a different congratulations message based on some action. The getRandomItem function accepts an array of items and uses the Math.random() function to select a random index, which can then be used to select a random element.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7ywVvGBb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di8qkkl6jy57n309ejzx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7ywVvGBb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di8qkkl6jy57n309ejzx.png" alt="Image description" width="802" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Remove Duplicate Elements&lt;/strong&gt;
Removing duplicate values from an array is a common task in JavaScript. For instance, you might be adding a user to another user's friends list, but you don't want to add that user more than once. The removeDuplicates function uses the Set constructor in JavaScript, which removes any duplicate (primitive) values by default. After that, we use the spread operator ... to spread the values into a new array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dkh_JQdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xob0fbxvmc1tzb1fz5k5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dkh_JQdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xob0fbxvmc1tzb1fz5k5.png" alt="Image description" width="802" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Wait for a Certain Period of Time&lt;/strong&gt;
The wait function accepts a parameter of the number of milliseconds to wait, and returns a promise that resolves after the specified amount of time has passed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To use the wait function, you can simply call it and provide the number of milliseconds to wait. You can then use the then callback or the await keyword to wait for the promise to resolve before continuing with the rest of your code.&lt;/p&gt;

&lt;p&gt;For example, if you want to wait for 2 seconds before navigating to a signup page, you can use the then callback like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bhKlRoM9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zv7jo0waxlbyfhwjpd5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bhKlRoM9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zv7jo0waxlbyfhwjpd5w.png" alt="Image description" width="802" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or you can use the await keyword like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QdZrBe2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bn21487wniuza02lcba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QdZrBe2I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bn21487wniuza02lcba.png" alt="Image description" width="802" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sort Elements By Certain Property&lt;/strong&gt;
A common task when displaying data in JavaScript is to sort it based on some property.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The below function uses the native sort method, compares the elements in the array based off of the provided key, and sorts the array in ascending order.&lt;/p&gt;

&lt;p&gt;Where would this be helpful? If you're fetching data that is supposed to be in a certain position based off of a position key, function will make sure that those elements are placed in the proper order.&lt;/p&gt;

&lt;p&gt;Here's an example code snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZC8ldKrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk9wol5afeektawdk5tt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZC8ldKrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk9wol5afeektawdk5tt.png" alt="Image description" width="802" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we define a function sortObjectsByProperty that takes in an array and a property name as arguments. Inside the function, we call the sort method on the array and pass in a compare function that compares the property values of two objects. The sorted array is then returned.&lt;/p&gt;

&lt;p&gt;In conclusion, JavaScript is a powerful and flexible programming language that can be used to create a wide variety of applications, from simple scripts to complex web applications. With its many built-in methods and functions, developers can write efficient and concise code that can be easily maintained and updated. The examples above demonstrate just a few of the many useful features and techniques that JavaScript has to offer. By mastering these techniques and continuing to learn and explore the language, developers can create truly remarkable applications and enhance their skills as software engineers.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Protected Routes in React Router V6</title>
      <dc:creator>Mehrab-18</dc:creator>
      <pubDate>Sat, 21 Jan 2023 07:28:53 +0000</pubDate>
      <link>https://dev.to/mehrab18/protected-routes-in-react-router-v6-12oa</link>
      <guid>https://dev.to/mehrab18/protected-routes-in-react-router-v6-12oa</guid>
      <description>&lt;p&gt;In React router, Private Routes commonly known as 'Protected Routes' are routes that requires a user being authorized to visit that route. Hence if a user is not authorized for a  page, they cannot access it.Authorization is far more than only authentication. For example, we can use it when we have roles and permissions in specific areas of the application.&lt;/p&gt;

&lt;p&gt;In this article, I want to share with you that how to implement Protected Routes in React Router V6?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Mehrab-18/react-protected-routes" rel="noopener noreferrer"&gt;Get Source Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites?&lt;/strong&gt; &lt;br&gt;
Make sure to install react-router and react-router-dom dependencies&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnaktjukp2o9cyl99o2az.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnaktjukp2o9cyl99o2az.png" alt="Image description" width="538" height="885"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have pages folder where we have all our components and a utils folder where we have a component with our authentication logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh5f6uyxqc56auazf0qzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh5f6uyxqc56auazf0qzn.png" alt="Image description" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start from wrapping your  component with  from react-router-dom&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F33wa2jc6nq2jzkvlr376.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F33wa2jc6nq2jzkvlr376.png" alt="Image description" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, setting up our App.js we will have  component that will contain all of our Routes. Now to protect some routes we will write them inside another  component ant this route will be our  component that will contain our logic &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ProtectedRoutes.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3zftmpp0mzsev5etwu9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3zftmpp0mzsev5etwu9b.png" alt="Image description" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This component will have a simple logic i.e. if user's authenticated direct it to the Child component that is our current route path and if a user isn't authenticated navigate it to our home page with path "/". Simple as that!&lt;/p&gt;

&lt;p&gt;Hopefully, this article helped you understand implementing protected routes in react router v6. After completely understanding you'll be able to implement &lt;strong&gt;Protected Routes&lt;/strong&gt; in your react app.&lt;/p&gt;

&lt;p&gt;Let's Build Safer React Apps!&lt;/p&gt;

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