<?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: Marlon Munoz</title>
    <description>The latest articles on Forem by Marlon Munoz (@marlonmunoz).</description>
    <link>https://forem.com/marlonmunoz</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%2F1812199%2F46a3b1d4-0788-4fb3-8701-ad5d6aea1875.jpeg</url>
      <title>Forem: Marlon Munoz</title>
      <link>https://forem.com/marlonmunoz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/marlonmunoz"/>
    <language>en</language>
    <item>
      <title>Differences between: forEach(), filter() and map() methods and its case uses.</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Mon, 10 Mar 2025 06:07:05 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/differences-between-foreach-filter-and-map-methods-and-its-case-uses-2o4m</link>
      <guid>https://forem.com/marlonmunoz/differences-between-foreach-filter-and-map-methods-and-its-case-uses-2o4m</guid>
      <description>&lt;p&gt;&lt;em&gt;Here are the use cases for &lt;code&gt;forEach()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt;in JavaScript:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forEach()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;forEach()&lt;/code&gt; when you need to execute a function on each element of an array without returning a new array.&lt;/li&gt;
&lt;li&gt;it is typically used for side effects, such as modifying each element, logging, or updating external variables
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1,2,3,4];
nums.forEach((num) =&amp;gt; {
    console.log(num * 2); // Side effect: logging each element multiplied by 2
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;map()&lt;/code&gt; when you need to create a new array containing only the elements that meet a certain condition.&lt;/li&gt;
&lt;li&gt;It returns a new array  with all the elements that pass the test implemented by the provided function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1, 2, 3, 4];
const evenNums = nums.filter((num) =&amp;gt; num % 2 === 0);
console.log(evenNums); // Output: [2, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;map()&lt;/code&gt; when you need to create a new array by transforming each element of the original array.&lt;/li&gt;
&lt;li&gt;It returns a new array with the results of calling a provided function on every element in the calling array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1, 2, 3, 4];
const halfNums = nums.map((num) =&amp;gt; num / 2);
console.log(halfNums); // Output: [0.5, 1, 1.5, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;The performance differences between &lt;code&gt;forEach()&lt;/code&gt;,&lt;code&gt;filter()&lt;/code&gt;, and &lt;code&gt;map()&lt;/code&gt; in JavaScript are generally minimal for most use cases, but there are some key points to consider:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;forEach()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt; &lt;code&gt;forEach()&lt;/code&gt; is typically the fastest of the three because it does not create a new array. It simply iterates over the existing array and performs the specified operation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Best used for operations that do not required a new array, such as logging, modifying elements in place, or performing side effects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; Fastest for side effects without creating a new array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;filter()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt; creates a new array containing only the elements that pass the test implemented by the provided function. This involves iterating over the array and potentially allocating memory for the new array, which can be slightly slower than &lt;code&gt;forEach()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Best used when you need to extract a subset of elements from an array based on a condition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; Slightly slower due to creating a new array with elements that pass a test&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;map()&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt; creates a new array with the result of calling a provided function on every element in the original array. This also involves iterating over the array, which can be slightly slower than &lt;code&gt;forEach()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Best used when you need to transform each element of an array into a new form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; Slightly slower due to creating a new array with transformed elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>REACT x FLASK setup</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Sun, 29 Sep 2024 06:04:26 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/react-x-flask-setup-3cm2</link>
      <guid>https://forem.com/marlonmunoz/react-x-flask-setup-3cm2</guid>
      <description>&lt;p&gt;&lt;strong&gt;WHAT IS FLASK?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;FLASK&lt;/strong&gt; is a lightweight web framework for &lt;strong&gt;Python&lt;/strong&gt; that allows you to build a web app quickly and with minimal boilerplate code. Let's go step-. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LETS START STEP-BY-STEP&lt;/strong&gt;&lt;br&gt;
Let's start by creating the necessary set up. &lt;a href="https://vitejs.dev/guide/" rel="noopener noreferrer"&gt;Go to Vite&lt;/a&gt; and copy this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use MAC only, so the setup for WINDOWS might be a bit different. Next, open up the terminal and paste the code you copy from Vite website. Once you run the code you will be prompt to 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;? Project name: › vite-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the &lt;code&gt;vite-project&lt;/code&gt; with your own project name. After you've given it a name, you are prompt to select the framework, in my case I will select REACT as my framework, but you can select your the framework you are familiar with and hit enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;? Select a framework: › - Use arrow-keys. Return to submit.
    Vanilla
    Vue
❯   React
    Preact
    Lit
    Svelte
    Solid
    Qwik
    Others
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you've selected your framework, now you can select your variant (language) of choice. I'm familiar with JavaScript so I will choose that. Remember: you need to choose the variant you dominate more and hit the enter tab.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;? Select a variant: › - Use arrow-keys. Return to submit.
    TypeScript
    TypeScript + SWC
❯   JavaScript
    JavaScript + SWC
    Remix ↗
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these prompts have been chosen, there are these following commands that are given to you to run them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scaffolding project in /Users/Marlon/Development/code/practice-phase-4/flask_app_dev/my-app...

Done. Now run:

  cd my-app
  npm install
  npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you ran those above codes successfully, you will arrive to your local host:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  VITE v5.4.8  ready in 1455 ms

  ➜  Local:   http://127.0.0.1:5555/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can copy the http address and paste it in your browser and you will see the Vite + React page, that you can now use for your project.&lt;/p&gt;

&lt;p&gt;Now open up your code editor, I'm using &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt; as my code editor. Once again, you can use your favorite editor. Next, inside VSCode's integrated terminal, you need to run these commands separately to get your local host address, to edit and start building your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's &lt;code&gt;cd&lt;/code&gt; inside &lt;em&gt;src&lt;/em&gt; and you will see the following files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src % tree
.
├── App.css
├── App.jsx
├── assets
│   └── react.svg
├── index.css
└── main.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside 'App.jsx' you can edit and/or erase the code inside that file and add your own code accordingly. These file contains the Vite and React logo.&lt;/p&gt;

&lt;p&gt;Once you have become familiar with the files you have created, now we can set up the frontend and backend directories. Let's create then by running this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir backend; mkdir server; mkdir frontend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's get our project setup to look more or less 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;my-app/
├── backend/
|       server/
│       ├── app.py
│       ├── models.py
│       ├── requirements.txt
├── frontend/
│   ├── src/
│   ├── public/
│   ├── package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open two terminal:&lt;br&gt;
One terminal will be for the &lt;code&gt;backend/server&lt;/code&gt; and the other will be for the &lt;code&gt;frontend/src&lt;/code&gt;.&lt;br&gt;
Inside &lt;code&gt;backend/server&lt;/code&gt; run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv install &amp;amp;&amp;amp; pipenv shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to ensure all the dependencies are install and to create our Pipfile.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;frontend/src&lt;/code&gt; run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to ensure all the necessary files such as our package.json files are created. &lt;/p&gt;

&lt;p&gt;Part 2 Coming....&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
    </item>
    <item>
      <title>PYTHON: OOP {Beginner's Edition}</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Mon, 09 Sep 2024 08:14:19 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/python-oop-beginners-edition-4ph5</link>
      <guid>https://forem.com/marlonmunoz/python-oop-beginners-edition-4ph5</guid>
      <description>&lt;p&gt;Python: Object-Oriented Programming [OOP]: is a programming paradigm (model) that uses &lt;strong&gt;objects&lt;/strong&gt; and &lt;strong&gt;classes&lt;/strong&gt; to structure software in a way that models real-world entities and relationships. This is based on the idea that objects can contain data and code that manipulates that data.&lt;/p&gt;

&lt;p&gt;There are key concepts you need to know about Object-Oriented Programming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Class&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attributes&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Methods&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inheritance&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encapsulation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Polymorphism&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Abstraction&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;The example below, is an example to help you reference the concepts being explained  and to give you a graphical idea of how each concept looks like inside of on object, if you are starting to learn OOP, this will help you grasp what each concept is doing.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car:
    def __init__(self, make, model, year, color):
        self.__make = make
        self.model = model
        self.year = year
        self.color = color

    def drive(self):
        print(f"The {self.year} {self.__make} {self.model} is driving")

# GETTER method for make
    def get_make(self):
        return self.__make

# SETTER method for make
    def set_make(self, make):
        self.__make = make


class ElectricCar(Car):
    def __init__(self, make, model, year, battery_size):
        super().__init__(make, model, year)
        self.battery_size = battery_size

    def drive(self):
        print(f"The {self.year} {self.make} {self.model} with a 
{self.battery_size}-kWh battery is driving silently.")

my_car = Car("McLaren Automotive", "Artura Spider", 2024, "Red/Black")
my_electric_car.drive()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is a Class:?&lt;/strong&gt;&lt;br&gt;
In Python, a &lt;strong&gt;Class:&lt;/strong&gt; is a blueprint for creating objects. It defines a set of attributes and methods that the created objects will have. For instance, you can quickly define a class 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;class Car:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is an Object?&lt;/strong&gt;&lt;br&gt;
In Python, and object is an instance of a class. It is created using the class blueprint  and can have its own unique data. For example you can create an object 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;my_car = Car("McLaren Automotive", "Artura Spider", 2024, "Red/Black")
my_electric_car
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What are Attributes?&lt;/strong&gt;&lt;br&gt;
In Python, attributes are variables that belong to a class or an object. They represent the state or data of the object. An example of an attribute looks 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;    self.color = color
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What are Methods?&lt;/strong&gt;&lt;br&gt;
In Python, methods are functions that belong to a class. They define behaviors or actions that the objects can perform. An example of a method looks like this: &lt;br&gt;
&lt;em&gt;TIP:  when defining a method, you can add &lt;code&gt;pass;&lt;/code&gt; temporally so that python won't throw an error when running the file.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    def drive(self):
        pass;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is Inheritance?&lt;/strong&gt;&lt;br&gt;
In Python, an Inheritance is a mechanism where a new class (child class) inherits attributes and methods from an existing class (parent class). An example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ElectricCar&lt;/code&gt; inherits from &lt;code&gt;Car&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ElectricCar(Car):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is Encapsulation?&lt;/strong&gt;&lt;br&gt;
Encapsulation in a bundling of data (attribute) and methods that operate on the data into a single unit (class), and restricting access to some of the object's components. In addition, you can make it a private variable by prefixing it with double underscores &lt;code&gt;__&lt;/code&gt;. Additionally, you should provide a getter and a setter methods to access and modify the private variable.&lt;br&gt;
For example, you can set a private variable 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;self.__make = make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add the Setter and Getter to access and modify the private variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # Getter method for make
    def get_make(self):
        return self.__make

    # Setter method for make
    def set_make(self, make):
        self.__make = make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Getter Method:&lt;/strong&gt; &lt;code&gt;get_make&lt;/code&gt; method is added to &lt;em&gt;access&lt;/em&gt; the private variable &lt;code&gt;__make&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setter Method:&lt;/strong&gt; &lt;code&gt;set_make&lt;/code&gt; method is added to &lt;em&gt;modify&lt;/em&gt; the private variable &lt;code&gt;__make&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This encapsulates the &lt;code&gt;make&lt;/code&gt; attribute, ensuring it is not directly accessible from outside the class, thus providing better encapsulation and control over the attribute. &lt;/p&gt;

</description>
      <category>python</category>
      <category>oop</category>
    </item>
    <item>
      <title>{useState} HooK { Briefly Explained};</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Mon, 09 Sep 2024 04:49:57 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/usestate-hook-briefly-explained-2di7</link>
      <guid>https://forem.com/marlonmunoz/usestate-hook-briefly-explained-2di7</guid>
      <description>&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a &lt;strong&gt;React&lt;/strong&gt; Hook that allows you to add state to your components by returning an array with two variables: &lt;code&gt;state, setState&lt;/code&gt;. The current state and the function that becomes the &lt;code&gt;setter&lt;/code&gt; function when it is called. It can be used to track data or properties that need to be tracked in an application, such as strings, numbers, booleans, arrays, or objects. &lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [state, setState] = useState();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple terms, &lt;code&gt;state&lt;/code&gt; will change at some any point and it needs to be updated, therefore 'setState' will update the &lt;code&gt;state&lt;/code&gt;, triggering a re-render of your components over time.&lt;/p&gt;

&lt;p&gt;In addition, useState can hold any kind of Javascript value, including objects. Something to ALWAYS keep in mind is that you should never change objects that you hold  in &lt;strong&gt;React&lt;/strong&gt; &lt;code&gt;state&lt;/code&gt; directly. First, you need to create a new one or create a copy  of an existing one and then &lt;code&gt;setState&lt;/code&gt; to the new copy. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Objects
const [state, setState] = useState({name: 'Marlo', age: 56});

const updateName = () =&amp;gt; {
  setState({...state, name: 'Karlo'});
};

const updateAge = () =&amp;gt; {
  setState({...state, age: 96});
};
---------------------------------------------------------------------------------
// Arrays
const [array, setArray] = useState([1, 2, 3, 4, 5]);

const addItem = () =&amp;gt; {
  setArray([...array, 6]);
};

const removeItem = () =&amp;gt; {
  setArray(array.slice(0, array.length - 1));
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use &lt;code&gt;useState&lt;/code&gt; in a &lt;strong&gt;React&lt;/strong&gt; component, first you need to import it form &lt;strong&gt;React&lt;/strong&gt; by writing the following code in the top of the component's page in two different ways, both work perfectly so you can choose your poison.&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 {useState} from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can write in one line&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, {useState} from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; Hook &lt;code&gt;useState&lt;/code&gt; can be called at the top level of a component or within custom hooks but not inside loops or conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [initialState, setInitialState] = useState();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;initialState&lt;/code&gt; is only used during the initial render and will be disregard in subsequent renders.&lt;/p&gt;

</description>
      <category>hooks</category>
      <category>react</category>
      <category>jsx</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React HooK= { Briefly Explained};</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Mon, 19 Aug 2024 05:17:50 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/react-hook-briefly-explained-4k74</link>
      <guid>https://forem.com/marlonmunoz/react-hook-briefly-explained-4k74</guid>
      <description>&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a &lt;strong&gt;React&lt;/strong&gt; Hook that allows you to add state to your components by returning an array with two variables: &lt;code&gt;state, setState&lt;/code&gt;. The current state and the function that becomes the &lt;code&gt;setter&lt;/code&gt; function when it is called. It can be used to track data or properties that need to be tracked in an application, such as strings, numbers, booleans, arrays, or objects. &lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [state, setState] = useState();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple terms, &lt;code&gt;state&lt;/code&gt; will change at some any point and it needs to be updated, therefore 'setState' will update the &lt;code&gt;state&lt;/code&gt;, triggering a re-render of your components over time.&lt;/p&gt;

&lt;p&gt;In addition, useState can hold any kind of Javascript value, including objects. Something to ALWAYS keep in mind is that you should never change objects that you hold  in &lt;strong&gt;React&lt;/strong&gt; &lt;code&gt;state&lt;/code&gt; directly. First, you need to create a new one or create a copy  of an existing one and then &lt;code&gt;setState&lt;/code&gt; to the new copy. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Objects
const [state, setState] = useState({name: 'Marlo', age: 56});

const updateName = () =&amp;gt; {
  setState({...state, name: 'Karlo'});
};

const updateAge = () =&amp;gt; {
  setState({...state, age: 96});
};
---------------------------------------------------------------------------------
// Arrays
const [array, setArray] = useState([1, 2, 3, 4, 5]);

const addItem = () =&amp;gt; {
  setArray([...array, 6]);
};

const removeItem = () =&amp;gt; {
  setArray(array.slice(0, array.length - 1));
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use &lt;code&gt;useState&lt;/code&gt; in a &lt;strong&gt;React&lt;/strong&gt; component, first you need to import it form &lt;strong&gt;React&lt;/strong&gt; by writing the following code in the top of the component's page in two different ways, both work perfectly so you can choose your poison.&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 {useState} from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or you can write in one line&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, {useState} from 'react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; Hook &lt;code&gt;useState&lt;/code&gt; can be called at the top level of a component or within custom hooks but not inside loops or conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [initialState, setInitialState] = useState();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;initialState&lt;/code&gt; is only used during the initial render and will be disregard in subsequent renders.&lt;br&gt;
The &lt;code&gt;initialState&lt;/code&gt; function is passed to the &lt;code&gt;setInitialState&lt;/code&gt; function, it takes the previous state as an argument, and returns a newState.&lt;/p&gt;

&lt;p&gt;Furthermore, in my opinion, there are no special rules about where you can and cannot use Hooks in React. Of course, you have to be cautious and tactical to keep you code organized.&lt;/p&gt;

&lt;p&gt;In one of my projects, building a SPA(Single Page App) there were various components to achieve my goal. The secret to be well organize, is to keep track of your components. For instance, your &lt;code&gt;App.js&lt;/code&gt; component will use &lt;code&gt;{useState}&lt;/code&gt; depending on what type of data needs updating.&lt;br&gt;
Let's introduce another powerful hook from &lt;strong&gt;React&lt;/strong&gt; called: &lt;code&gt;{useEffect}&lt;/code&gt; and use it along &lt;code&gt;{useState}&lt;/code&gt; to explain how these hook perform operations on data. The following example comes from my &lt;code&gt;App.js&lt;/code&gt; component I used in a recent project. I was working with a &lt;code&gt;db.json file&lt;/code&gt; data for toys that will help children development for the first year. This is my endpoint &lt;code&gt;http://localhost:4000/toys&lt;/code&gt; to help you understand the process of how &lt;code&gt;{useState}&lt;/code&gt; and &lt;code&gt;{useEffect}&lt;/code&gt; work inside inside of an application component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First:&lt;/strong&gt; Initialize State:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [toys, setToys] = useState([]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This line initializes a state variable toys with an empty array &lt;code&gt;[]&lt;/code&gt; as the initial value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setToys&lt;/code&gt; is a function that will be used to update the toys state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Second:&lt;/strong&gt; Fetch Data on Component Mount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {             
    fetch("http://localhost:4000/toys")  
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setToys(data)); 
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;{useEffect}&lt;/code&gt; hook is used to perform side effects in the component.&lt;/li&gt;
&lt;li&gt;The function inside &lt;code&gt;{useEffect}&lt;/code&gt; will run once when the component mounts because the dependency array &lt;code&gt;([])&lt;/code&gt; is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Third:&lt;/strong&gt; Fetch Toys Data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fetch("http://localhost:4000/toys")&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;This line makes a GET request to the specified URL to fetch toys data.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;.then(response =&amp;gt; response.json())&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;the response from the fetch request is converted to JSON format.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;.then(toysData =&amp;gt; setToyData(toysData));&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The JSON data &lt;code&gt;(toysData)&lt;/code&gt; is used to update the &lt;code&gt;toys&lt;/code&gt; state using the &lt;code&gt;setToys&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To understand more in depth how both &lt;code&gt;{useState, useEffect}&lt;/code&gt; work you can visit the official &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React&lt;/a&gt; website. In addition, another helpful source is &lt;a href="https://www.w3schools.com/react/default.asp" rel="noopener noreferrer"&gt;w3schools&lt;/a&gt; website, that is my personal favorite. It goes straight to the point with examples that you can try in their own browser. Lastly, if you need a more technical source, the &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_getting_started" rel="noopener noreferrer"&gt;mdn web docs&lt;/a&gt; will help you. &lt;/p&gt;

</description>
      <category>hooks</category>
      <category>react</category>
      <category>jsx</category>
      <category>javascript</category>
    </item>
    <item>
      <title>let, var or const, what's the difference?</title>
      <dc:creator>Marlon Munoz</dc:creator>
      <pubDate>Sun, 28 Jul 2024 07:24:11 +0000</pubDate>
      <link>https://forem.com/marlonmunoz/let-var-or-const-whats-the-difference-33ll</link>
      <guid>https://forem.com/marlonmunoz/let-var-or-const-whats-the-difference-33ll</guid>
      <description>&lt;p&gt;&lt;strong&gt;let, var , const?&lt;/strong&gt; when do we use this variables really? &lt;br&gt;
Back in 2018, I got the opportunity to play around a little bit with JavaScript. During that year, I was thrilled to learn how this language performs. The first thing I learned was to declare a variable. My first variable was &lt;code&gt;var name = 'el marlo'&lt;/code&gt; after I so this variable be used within my function and &lt;code&gt;console.log(name) // =&amp;gt; el marlo&lt;/code&gt;. As a beginner programmer, I got excited. Fast forward to 2024, officially starting my journey into Software Engineering, getting my hands into JavaScript after so many years and a lot of this have changed. The first thing I noticed, there were more options to declare a variable? What is &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;, I was only familiar with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;var&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; is the oldest keyword for declaring variable. Therefore, let's address the difference against the other two: &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; to help us decide which one should go into our code.&lt;/p&gt;

&lt;p&gt;I learned that &lt;code&gt;var&lt;/code&gt; was a keyword that if you are planning to use, to be very careful or don't use it at all due to a &lt;u&gt;lacking in block scope&lt;/u&gt; or in plain english, the code that goes inside the curly braces {}. In addition, it can create bugs in your code because &lt;code&gt;var&lt;/code&gt; variables can be re-declared and updated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var favHobby = "Eskate";
var favHobby = "Sleeping";
var favHobby = "Joking";

console.log(favHobby); // =&amp;gt; Joking
console.log(favHobby); // =&amp;gt; Joking
console.log(favHobby); // =&amp;gt; Joking

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;let&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt;is the update version of &lt;code&gt;var&lt;/code&gt;. This variable is &lt;u&gt;blocked scoped&lt;/u&gt;, which means that unlike &lt;code&gt;var&lt;/code&gt;, anything that we declare  within &lt;code&gt;{curly braces will only be available within this scope}&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

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

if (x === 1) {
  let x = 2;

  console.log(x);
  // Expected output: 2
}

console.log(x);
// Expected output: 1

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="noopener noreferrer"&gt;Example from: mdn web docs&lt;/a&gt;&lt;br&gt;
In addition, &lt;code&gt;let&lt;/code&gt; &lt;u&gt;can be updated but NOT re-declared&lt;/u&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;const&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; is the more reliable variable to used for the following reasons: &lt;code&gt;const&lt;/code&gt; &lt;u&gt;declarations are block scoped&lt;/u&gt;: which means it is only accessible &lt;code&gt;{within the block}&lt;/code&gt;. Another strong reason is, &lt;code&gt;const&lt;/code&gt; cannot be updated or re-declared unless it is an object. If &lt;code&gt;const&lt;/code&gt; was an  &lt;code&gt;object&lt;/code&gt;, then properties could be added, removed or updated.&lt;br&gt;
&lt;/p&gt;

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

try {
  number = 99;
} catch (err) {
  console.log(err);
  // Expected output: TypeError: invalid assignment to const 'number'
  // (Note: the exact output may be browser-dependent)
}

console.log(number);
// Expected output: 42

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const" rel="noopener noreferrer"&gt;Example from: mdn web docs&lt;/a&gt;&lt;/p&gt;

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