<?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>Forem: Charles 🛰</title>
    <description>The latest articles on Forem by Charles 🛰 (@lautaro).</description>
    <link>https://forem.com/lautaro</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%2F810914%2F1855e971-314e-44ef-964c-f1285abe37d9.jpg</url>
      <title>Forem: Charles 🛰</title>
      <link>https://forem.com/lautaro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lautaro"/>
    <language>en</language>
    <item>
      <title>Dynamic routes in react-router v6</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Thu, 10 Mar 2022 08:44:12 +0000</pubDate>
      <link>https://forem.com/lautaro/dynamic-routes-in-react-router-v6-569m</link>
      <guid>https://forem.com/lautaro/dynamic-routes-in-react-router-v6-569m</guid>
      <description>&lt;p&gt;Hey I found my self with a problem of not knowing how to do dynamic routes in react router v6 so here it is a short tutorial&lt;/p&gt;

&lt;p&gt;In the file where you have your routes let's make a new dynamic routes such like this&lt;/p&gt;

&lt;p&gt;App.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route index element={&amp;lt;Main/&amp;gt;}/&amp;gt;
        &amp;lt;Route path='/main' element={&amp;lt;Home /&amp;gt;}/&amp;gt;
        &amp;lt;Route path='/main/:id' element={&amp;lt;ProductPage/&amp;gt;}/&amp;gt;
      &amp;lt;/Routes&amp;gt;
      &amp;lt;/BrowserRouter&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will notice that the dynamic route is the one with the :id, once we got this we should go to the component page which in this case is ProductPage&lt;/p&gt;

&lt;p&gt;ProductPage.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useParams } from 'react-router-dom';
import React, { useContext } from 'react';
import shopContext from '../context/shopContext';

const ProductPage = () =&amp;gt; {
    const state = useContext(shopContext)
    const { id } = useParams();
    const product = state.list.find((p: any) =&amp;gt; p.id === id)

    return(
        &amp;lt;div&amp;gt;
            &amp;lt;h1 style={{color: 'white'}}&amp;gt;{product.title}&amp;lt;/h1&amp;gt;

        &amp;lt;/div&amp;gt;
    )
}

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

&lt;/div&gt;



&lt;p&gt;Here you will see a lot but the main thing here is the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const { id } = useParams();
    const product = state.list.find((p: any) =&amp;gt; p.id === id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is where with useParams we know the url id and then on the product constant we compare it with the data from the api or mock data.&lt;/p&gt;

&lt;p&gt;Once we got that from the product constant we can access to the data from our api like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return(
        &amp;lt;div&amp;gt;
            &amp;lt;h1 style={{color: 'white'}}&amp;gt;{product.title}&amp;lt;/h1&amp;gt;

        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now how to pass the data and navigate to the right page? Let's see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;Link to={`/main/${data.id}`}&amp;gt;
 &amp;lt;/Link&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above we pass the id of the product when we are map all the data, so depends on what product do we click it will pass the id of the right product.&lt;/p&gt;

&lt;p&gt;Hope someone found it helpful.&lt;/p&gt;

&lt;p&gt;Lautaro&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>route</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to navigate to the previos page with react-router v6</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Tue, 08 Mar 2022 11:10:16 +0000</pubDate>
      <link>https://forem.com/lautaro/how-to-navigate-to-the-previos-page-with-react-router-v6-4b9c</link>
      <guid>https://forem.com/lautaro/how-to-navigate-to-the-previos-page-with-react-router-v6-4b9c</guid>
      <description>&lt;p&gt;If found an approach that i do not like that much but it works&lt;/p&gt;

&lt;p&gt;So if you want to return to the previous page using react router v6 you can do the following&lt;br&gt;
&lt;/p&gt;

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

const Component = () =&amp;gt; {
   const navigate = useNavigate()
  return(
   &amp;lt;div&amp;gt;
     &amp;lt;button onClick={() =&amp;gt; navigate(-1)}
   &amp;lt;/div&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope someone found it helpful.&lt;/p&gt;

&lt;p&gt;Lautaro&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>How to know your url location on react (with react-router-dom)</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Thu, 03 Mar 2022 15:59:36 +0000</pubDate>
      <link>https://forem.com/lautaro/how-to-know-your-url-location-on-react-with-react-router-dom-2loh</link>
      <guid>https://forem.com/lautaro/how-to-know-your-url-location-on-react-with-react-router-dom-2loh</guid>
      <description>&lt;p&gt;In this post we will be able to see how to know the url we are currently on so for example if we have to fire an event depending on the url we can do it.&lt;/p&gt;

&lt;p&gt;First of all we need to have installed or we have to be using react-router-dom&lt;/p&gt;

&lt;p&gt;If you already got it let's begin&lt;/p&gt;

&lt;p&gt;First import it at the top of your js or ts file&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Once you import it we can call it like this inside the component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const location = useLocation();

  useEffect(() =&amp;gt; {
    console.log(location.pathname)
}, [])
return(
  &amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you go to the console on the browser you will be able to see the path you currently at.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Absolute imports (in react)</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Wed, 23 Feb 2022 09:58:54 +0000</pubDate>
      <link>https://forem.com/lautaro/absolute-imports-in-react-14a0</link>
      <guid>https://forem.com/lautaro/absolute-imports-in-react-14a0</guid>
      <description>&lt;p&gt;Hey there, some time ago i found my self with imports like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Card from "../../../components/card";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which is quite a mess and not clear at all.&lt;/p&gt;

&lt;p&gt;But here is where absolute imports came to rescue me.&lt;/p&gt;

&lt;p&gt;The first thing we have to do is create a file, if you`re using **javascript **the name of your file will be jsconfig.json&lt;br&gt;
and the file have to be at the same level as package.json.&lt;br&gt;
Inside of it we will add this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
{&lt;br&gt;
  "compilerOptions": {&lt;br&gt;
    "baseUrl": "src"&lt;br&gt;
  },&lt;br&gt;
  "include": ["src"]&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you`re using &lt;strong&gt;typeScript&lt;/strong&gt; you already should have the file which is tsconfig.json and you will have to add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    ...,
    "baseUrl": "src"
  },
  "include": [
    "src"
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have out jsconfig.json file set up we can do our imports.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Different ways of export and import</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Wed, 23 Feb 2022 09:51:33 +0000</pubDate>
      <link>https://forem.com/lautaro/different-ways-of-export-and-import-3911</link>
      <guid>https://forem.com/lautaro/different-ways-of-export-and-import-3911</guid>
      <description>&lt;p&gt;Here we will talk about how and when should we use the import without brackets like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Card from "components/card";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and when we have to use the import with brackets like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Card } from "components/card";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It depends on how we made the export, if we made a export default we will have to import without brackets like in the first example&lt;/p&gt;

&lt;p&gt;whereas if we made an export like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Card = () =&amp;gt; {
 return(

)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;our import will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Card } from "components/card";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>How to solve fu***** cors?</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Mon, 14 Feb 2022 10:29:49 +0000</pubDate>
      <link>https://forem.com/lautaro/how-to-solve-fu-cors-39f6</link>
      <guid>https://forem.com/lautaro/how-to-solve-fu-cors-39f6</guid>
      <description>&lt;p&gt;I will document how to solve cors in a few steps.&lt;/p&gt;

&lt;p&gt;Imagine you have an api where you want to POST or GET things but when you post or get something you receive a CORS error policy, if you want a deep understanding of cors i will leave this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Imagine you have an api at localhost:9000 and all the different routes related to it.&lt;/p&gt;

&lt;p&gt;We will have to create a proxy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir proxy
cd proxy
npm init -y
npm i express
npm i http-proxy-middleware nodemon --save-dev
touch index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have installed our dependencies we can go to the code.&lt;/p&gt;

&lt;p&gt;Inside package.json add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "nodemon index.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now inside index.js we 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;const express = require('express');
const app = express()
const { createProxyMiddleware } = require('http-proxy-middleware');

app.use('/', createProxyMiddleware({target: 'http://localhost:9000', changeOrigin:true}))

app.listen(5000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way we will be able to make request to the url localhost:5000 and since it is a proxy we will be able to make request to the routes that the original route has for instance the main route is localhost:9000/api/users, but now in order to not get cors policy we will have to make a call to localhost:5000/api/users and it should work just fine.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>cors</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Basic git command you have to know.</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Tue, 08 Feb 2022 23:08:16 +0000</pubDate>
      <link>https://forem.com/lautaro/basic-git-command-you-have-to-know-18b9</link>
      <guid>https://forem.com/lautaro/basic-git-command-you-have-to-know-18b9</guid>
      <description>&lt;p&gt;I will talk about the basic commands about GIT you have to know.&lt;/p&gt;

&lt;p&gt;First you need to have installed git.&lt;/p&gt;

&lt;p&gt;For now on i will assume that you have installed git.&lt;/p&gt;

&lt;p&gt;There are several ways to use git but i will show in the two more basic ways you would need to use it.&lt;/p&gt;

&lt;p&gt;Maybe you saw a github repository that you want to saw at your code editor or you want to test it localy or for work or whatever reason you have.&lt;br&gt;
You will need to run on the console the following command&lt;br&gt;
&lt;code&gt;git clone https://github.com/lautaroCharles/typeApp.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will have to replace the url with the url you want.&lt;/p&gt;

&lt;p&gt;Once you run the command you will be able to cd inside the folder and be able to see the code or even run it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let's talk about if you want to publish your code on github&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First thing you will need is to be inside your project folder. Once you are there you will have to run the following command &lt;br&gt;
&lt;code&gt;git init&lt;/code&gt;&lt;br&gt;
This command will initialize git.&lt;/p&gt;

&lt;p&gt;After that you will need to run a command to add your code into the staging area&lt;br&gt;
&lt;code&gt;git add .&lt;/code&gt;&lt;br&gt;
the command above will add your files into the stage area, but maybe you just want one file, for that you will have to run this&lt;br&gt;
&lt;code&gt;git add index.js&lt;/code&gt;&lt;br&gt;
This way you just will add the changes you made inside index.js&lt;/p&gt;

&lt;p&gt;Now that you have your code on staging area we can make a commit with a clear comment, in other blog i will explain how to make clear commits.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit -m 'first commit'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we just have two more steps&lt;br&gt;
&lt;code&gt;git remote add origin https://github.com/lautaroCharles/typeApp.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On the url you will put your empty repository so after that we can push our code like this &lt;br&gt;
&lt;code&gt;git push -u origin main&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we just have to go to our github repository and see our upload&lt;/p&gt;

&lt;p&gt;I will leave some other useful commands&lt;br&gt;
git status: See what is the status of your code, if you have something on staging or something that you can pass into staging.&lt;br&gt;
git branch: See what branches do you have into your repository&lt;br&gt;
git checkout main: (main is the branch name which can have another name) With this command you can change the branch that you currently are.&lt;/p&gt;

&lt;p&gt;I will leave this link which a bunch of other git commands &lt;a href="https://www.loginradius.com/blog/async/git-commands/"&gt;link&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>useCallback and useMemo?</title>
      <dc:creator>Charles 🛰</dc:creator>
      <pubDate>Mon, 07 Feb 2022 14:27:42 +0000</pubDate>
      <link>https://forem.com/lautaro/usecallback-and-usememo-5ib</link>
      <guid>https://forem.com/lautaro/usecallback-and-usememo-5ib</guid>
      <description>&lt;p&gt;Lets's talk about useCallback and useMemo, both of them are really similar.&lt;br&gt;
UseCallback is used to optimize the rendering behavior of your React function components, while useMemo is used to memoize expensive functions to avoid having to call them on every render.&lt;br&gt;
So what means that?&lt;/p&gt;

&lt;p&gt;The first difference between them is the syntax, useMemo do not take in consideration the arguments whereas useCallback do take arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoUsed() {
  const a  = useMemo((arg1) =&amp;gt; {
    // React ignores arguments
    return ‘insert JSX here’
  }, [])

  return a
}

function callbackUsed() {
  const a = useCallback((what, where) =&amp;gt; {
    // can be used inside functions
    return ‘insert ${what} ${where}’
  })

  return a(‘JSX’, ‘here’)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;useMemo memoize values whereas in useCallback you can not.&lt;/p&gt;

&lt;p&gt;useCallback gives you referential equality between renders for functions. And useMemo gives you referential equality between renders for values.&lt;/p&gt;

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