<?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: M.Ark </title>
    <description>The latest articles on Forem by M.Ark  (@ark7).</description>
    <link>https://forem.com/ark7</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%2F832101%2F0cd025fb-0184-4844-bf45-8e7d53668c3c.png</url>
      <title>Forem: M.Ark </title>
      <link>https://forem.com/ark7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ark7"/>
    <language>en</language>
    <item>
      <title>Classes in python</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Fri, 10 Apr 2026 10:20:14 +0000</pubDate>
      <link>https://forem.com/ark7/classes-in-python-21nc</link>
      <guid>https://forem.com/ark7/classes-in-python-21nc</guid>
      <description>&lt;p&gt;In the real world, the physical world is made up of material items for example wood, metal,plastic etc. We have to think of these in higher terms to as to understand. For example we think of things as chairs, tables, house etc. Same thing in the programming field,programmers deal with higher level groupings of items-objects.&lt;/p&gt;

&lt;p&gt;In programming an object is a grouping of data and operations that can be performed on the data. &lt;br&gt;
Encapsulation occurs when a user interacts with an objects at a higher level , allowing the lower level details to be hidden. This is also known as information hiding or encapsulation. Objects support abstraction by hiding entire groups of functions and variables and exposing only certain functions to a user. &lt;/p&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Abstraction simplifies our world. An oven is viewed as having a compartment for food and a knob is turned to heat the food.&lt;/li&gt;
&lt;li&gt;People need not be concerned with an oven's internal workings. Ex: People don't reach inside to adjust the flame. &lt;/li&gt;
&lt;li&gt;Similarly, an object has operations that a user applies. The object's internal data, and possibly other operations, are hidden from the user. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Multiple variables are frequently closely related and should be treated as one variable with multiple parts. For example, two variables called hours and minutes might be grouped together in a single variable called time. The &lt;strong&gt;Class&lt;/strong&gt; keyword can be used to create a user-defined type of object containing groups of related variables and functions. &lt;/p&gt;
&lt;h2&gt;
  
  
  syntax
&lt;/h2&gt;

&lt;p&gt;class ClassName:&lt;br&gt;
    # Statement-1&lt;br&gt;
    # Statement-2&lt;br&gt;
    # ...&lt;br&gt;
    # Statement-N&lt;/p&gt;

&lt;p&gt;A class defines a new type that can group data and functions to form an object. The object maintains a set of attributes that determines the data and behavior of the class. &lt;br&gt;
Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
         &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
         &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;From the sample code above, a programmer can the use instantiation to define  anew Time class variable and access the variable attributes.&lt;br&gt;
An instantiation operation is performed by "calling" the class, using parentheses like a function call as in my_time = Time().&lt;/p&gt;

&lt;p&gt;An instantiation operation creates an instance, which is an individual object of the given class. An instantiation operation automatically calls the &lt;em&gt;init&lt;/em&gt; method defined in the class definition. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;method&lt;/strong&gt; is a function defined within a class. The &lt;em&gt;init&lt;/em&gt; method, commonly known as a constructor, is responsible for setting up the initial state of the new instance. In the example above, the &lt;strong&gt;init&lt;/strong&gt; method creates two new attributes, hours and minutes, and assigns default values of 0. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;init&lt;/strong&gt; method has a single parameter, &lt;em&gt;"self"&lt;/em&gt;, that automatically references the instance being created. To create a new attribute, hours, we write an expression such as self.hours = 0 within the &lt;strong&gt;init&lt;/strong&gt; method. &lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;


&lt;span class="n"&gt;my_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;my_time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="n"&gt;my_time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;my_time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; hours&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;my_time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; minutes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the code above will be 5 hours and 46 minutes.&lt;/p&gt;

&lt;p&gt;In the code sample above, we have used instantiation to create a variable using the Time class.&lt;br&gt;
In the same manner, we can create multiple instances of a class in a program, with each instance having different attribute values. &lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;


&lt;span class="n"&gt;time1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# instance called time1
&lt;/span&gt;&lt;span class="n"&gt;time1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;time1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;39&lt;/span&gt;

&lt;span class="n"&gt;time2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# a second instance called time2
&lt;/span&gt;&lt;span class="n"&gt;time2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="n"&gt;time2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; hours and &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; minutes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; hours and &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; minutes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The output of the code above is  3hours and 39 minutes &lt;br&gt;
4 hours and 58 minutes.&lt;/p&gt;

&lt;p&gt;In the code above we have two instances of the Time class being instantiated. As we said initially, instantiation is done by calling the class.&lt;br&gt;
We have time1 and time2 being the instances of the Time class.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>React Component life cycle</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 19 Nov 2025 07:31:15 +0000</pubDate>
      <link>https://forem.com/ark7/react-component-life-cycle-f6n</link>
      <guid>https://forem.com/ark7/react-component-life-cycle-f6n</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/ark7" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F832101%2F0cd025fb-0184-4844-bf45-8e7d53668c3c.png" alt="ark7"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/ark7/react-component-life-cycle-1448" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;React Component life cycle&lt;/h2&gt;
      &lt;h3&gt;M.Ark  ・ Nov 19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#resources&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#newbie&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>resources</category>
      <category>newbie</category>
    </item>
    <item>
      <title>React Component life cycle</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 19 Nov 2025 07:07:57 +0000</pubDate>
      <link>https://forem.com/ark7/react-component-life-cycle-1448</link>
      <guid>https://forem.com/ark7/react-component-life-cycle-1448</guid>
      <description>&lt;p&gt;Just as life has different phases involved, i.e we are born, grow and later die, React components experience phases as well. The phases which react components undergo are mounting, updating and unmounting. &lt;/p&gt;

&lt;p&gt;Components interact with components to provide an interface for user interaction in web applications. However, a component – the building block of every UI we interact with – has a life cycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets go through the phases briefly.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Mounting phase
&lt;/h2&gt;

&lt;p&gt;This is the first phase in a react component. We can as well say it is the birth phase of a react component. In this phase, a component instance is created and inserted into the DOM.(Document Object Model). We have 4 methods in this phase listed below and in their order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. constructor()&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Constructor method is called in the mounting phase and before the component is mounted. When we call the constructor method, it accepts props as arguments. The constructor method serves 2 purposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It initializes a local state in the class component.  This is done by assigning an object to &lt;em&gt;this.state&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The constructor method binds event handler methods&lt;br&gt;
That's it for the constructor method on our mounting phase&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. static getDerivedStateFromProps()&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The method listed above is invoked immediately before we render the elements in the Document Object Model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_3. render() _&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To render basically means to show.&lt;br&gt;
The render() method is responsible for displaying the component on the screen.&lt;br&gt;
This method is required for outputting React HTML elements. Both JavaScript and HTML are rendered through this method. What we see on our interface depends on what this render() method returns in the form of JSX. &lt;br&gt;
It returns the JSX (React’s combination of HTML and JavaScript) that should appear in the UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;4. componentDidMount()&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Immediately after the React component is renderedinto the DOM tree, this method is initiated, just after the first render method is invoked. The componentDidMount()method allows you to execute user actions and other side effects once components are mounted.&lt;/p&gt;

&lt;p&gt;This is it for our mounting phase and I hope it is clearer now.&lt;br&gt;
We can now move to our second phase of the react component lifecycle.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Updating phase
&lt;/h2&gt;

&lt;p&gt;Immediately after the mounting in the react component lifecycle has happened, the updating phase takes over. This is the where growing of the React component happens. In this phase, the commonly used method is the &lt;em&gt;componentDidUpdate()&lt;/em&gt; method. &lt;br&gt;
An update of the react component is done when we either have changes in the props or the data state. &lt;br&gt;
The state is re-rendered using the &lt;em&gt;render()&lt;/em&gt; method, and the updated state is returned to the UI.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Unmounting phase
&lt;/h2&gt;

&lt;p&gt;Unmount is basically to detach. In this phase it is the death phase of the component lifecycle process. &lt;br&gt;
In this phase, the method componentWillUnmount() is invoked immediately before the component is unmounted from the DOM. &lt;br&gt;
In an example, a user accessing a user interface come in contract with buttons, forms and many more in our applications.  Users tend to move from one component interaction to another and, on the whole, have a reactive experience.&lt;br&gt;
At every point in the user interaction cycle, the React components are either inserted into the DOM tree or state changes, or the completion of component’s lifecycle. &lt;/p&gt;




&lt;p&gt;In conclusion, briefly we have gone through various life cycle methods in React class components and why each of them exists in rendering React components. &lt;/p&gt;




&lt;p&gt;See you in the next one and happy coding!! &lt;/p&gt;




</description>
      <category>react</category>
      <category>resources</category>
      <category>newbie</category>
    </item>
    <item>
      <title>How To Deal With Side Effects</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 10 Jul 2024 08:29:27 +0000</pubDate>
      <link>https://forem.com/ark7/how-to-deal-with-side-effects-5f22</link>
      <guid>https://forem.com/ark7/how-to-deal-with-side-effects-5f22</guid>
      <description>&lt;p&gt;Dealing with side effects in React is crucial to ensuring your components behave correctly and efficiently. React provides several hooks and lifecycle methods to handle side effects. &lt;/p&gt;

&lt;p&gt;Certain components in React need to interact with things outside themselves. These things can be anything from querying data from a server to finding/changing the position of the component on the webpage or even sending some data to a server when necessary. This interaction with the outside world is called a &lt;strong&gt;side-effect&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Though we are already familiar with rendering code and adding event handlers, it is not enough for all uses, like when you want to connect to your server and fetch messages to show to a user. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effects&lt;/strong&gt; let you run some code to synchronize your component as necessary, on rendering or a reactive/state value change rather than on a particular event.&lt;/p&gt;

&lt;p&gt;Similar to how we have the &lt;em&gt;useState&lt;/em&gt; hook, React offers us a handy useEffect hook to use effects in our components.&lt;/p&gt;

&lt;p&gt;Here's an overview of how to manage side effects in functional components using hooks and in class components using lifecycle methods:&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%2Fjeze48juxnf6x6zppqrj.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%2Fjeze48juxnf6x6zppqrj.png" alt=" " width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run the code above, you do realize that the count in our wepage is inconsistenmt and it runs crazy, this implementation has a problem: _setInterval _is called every time the component renders, which will create multiple intervals and can cause performance issues.&lt;/p&gt;

&lt;p&gt;This is where the useEffect hook swoops in to save us. We can wrap this calculation inside a useEffect hook to move it outside the rendering calculation. It accepts a callback function with all the calculations.&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%2F36l412o3jshlqolh1hev.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%2F36l412o3jshlqolh1hev.png" alt=" " width="800" height="592"&gt;&lt;/a&gt;&lt;br&gt;
In the code above , we have added the useEffect function but the result from our page still runs crazy. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;useEffect&lt;/em&gt; is a hook that lets us perform side effects in function components. Here, we use it to set up an interval that increments the counter every second.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;setInterval&lt;/em&gt; function runs the provided function (which increments counter) every 1000 milliseconds (1 second). &lt;/p&gt;

&lt;p&gt;The return statement inside &lt;em&gt;useEffect&lt;/em&gt; is a cleanup function that clears the interval when the component is unmounted to prevent memory leaks.&lt;/p&gt;
&lt;h2&gt;
  
  
  The dependency array
&lt;/h2&gt;

&lt;p&gt;Effect Runs on Every Render: Without the dependency array, useEffect runs after every render. &lt;/p&gt;

&lt;p&gt;This means that every time the component re-renders (which happens whenever the state or props change), a new interval is set up. This leads to multiple intervals being created, causing the counter to increment much faster than intended.&lt;/p&gt;

&lt;p&gt;Fortunately, the second argument accepts an array of dependencies allowing the hook to re-render only when those dependencies are changed. So if you have a state variable and want to have some side-effect occur any time the state changes, you can use this hook and mention the state variable in the dependency array.&lt;/p&gt;

&lt;p&gt;We pass an empty array in this example because we do not want the useEffect hook to run anytime other than the initial component render.&lt;br&gt;
Here is how we pass an empty array.&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%2F4khes7ebledvsbuhlexl.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%2F4khes7ebledvsbuhlexl.png" alt=" " width="800" height="592"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code sample above, by including the dependency array,we ensure that the interval is set up only once when the component mounts and not on every render. However, it is crucial to add cleanup for the interval to prevent potential memory leaks or performance issues.&lt;/p&gt;

&lt;p&gt;As seen now, our application counts seconds without being messy but we still have an issue with the counter updating twice every second though. That can be understood as a behavior caused by the React StrictMode.&lt;br&gt;
In our code, every time the useEffect hook runs, a new setInterval is used. When the component is unmounted, setInterval is not stopped, it keeps incrementing. This unnecessary behavior can be prevented by clearing the interval when the component is unmounted and that is where the third part of our useEffect hook comes in - the cleanup function.&lt;/p&gt;

&lt;p&gt;You can return a function from the callback in the useEffect hook, which will be executed each time before the next effect is run, and one final time when the component is unmounted. In this case, let us clean up the interval with a cleanup function.&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%2Fqtjn0b18q041gfmoexij.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%2Fqtjn0b18q041gfmoexij.png" alt=" " width="800" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In our code provided above:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;useEffect is a hook that lets us perform side effects in function components. Here, it is used to set up an interval.&lt;/p&gt;

&lt;p&gt;Inside useEffect, the setInterval function creates an interval that increments the counter state by 1 every 1000 milliseconds (1 second).&lt;/p&gt;

&lt;p&gt;The const key = setInterval(...) line assigns the interval ID to a variable named key.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;return statement **inside useEffect is a cleanup function that clears the interval when the component **unmounts&lt;/strong&gt;. This prevents memory leaks by ensuring that the interval is properly cleaned up.&lt;/p&gt;

&lt;p&gt;The empty dependency array [] ensures that this effect runs only once, after the initial render of the component.&lt;/p&gt;
&lt;h2&gt;
  
  
  NB
&lt;/h2&gt;

&lt;p&gt;By including the cleanup function inside the useEffect, we ensure that the interval is properly cleared when the component is unmounted, preventing potential memory leaks and ensuring efficient use of resources.&lt;/p&gt;

&lt;p&gt;This is a summary of how useEffect hook:&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; {
    // execute side effect
    return () =&amp;gt; {
      // cleanup function on unmounting or re-running effect
    }
  },
  // optional dependency array
  [/* 0 or more entries */]
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cases where useEffect does not need to be used.
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You do not need to use an effect if you are only calculating something based on the state during rendering. For a change in a component, due to a change in the props, you can calculate and set it during rendering.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You do not need effects for events. Code that runs when a component is displayed should be in effects, the rest should be in events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You do not need an effect to reset the state based on a condition most of the time. You have learned about keys in React. Just like using a key on a list’s item, adding one to a component, based on the state on which it should be reset creates a unique version of that component for each change in the value of the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you are having issues with managing your state and want to use an effect to update the state of a parent or some other non-child component, consider lifting the state. As we know, in React, the state flows in one direction, generally down the DOM. So the parents know of the data before passing it to the children. If multiple children are required to make use of a single state, it should be moved up to the parent that has all of the components that need it, instead of using escape hatches like an effect.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That has brought us to the end of our learning on useEffect in React.&lt;br&gt;
In case of any questions let me know and we shall go through them one by one. &lt;/p&gt;

&lt;p&gt;Happy hacking!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>More on React States</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Mon, 01 Jul 2024 21:01:30 +0000</pubDate>
      <link>https://forem.com/ark7/more-on-react-states-4p1</link>
      <guid>https://forem.com/ark7/more-on-react-states-4p1</guid>
      <description>&lt;p&gt;This section contains a general overview of topics that you will learn in this lesson.&lt;br&gt;
    How to structure state.&lt;br&gt;
    How state updates.&lt;br&gt;
    Learn about controlled components.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to structure state
&lt;/h2&gt;

&lt;p&gt;Managing and structuring state effectively is by far one of the most crucial parts of building your application. If not done correctly, it can become a source of bugs and headaches. Poor state management can lead to unnecessary &lt;strong&gt;re-renders&lt;/strong&gt;, &lt;strong&gt;complex&lt;/strong&gt; and &lt;strong&gt;hard-to-debug&lt;/strong&gt; code, and unpredictable application behavior.&lt;/p&gt;

&lt;p&gt;The assignment items go through the topic thoroughly, but as a general rule of thumb: &lt;strong&gt;don’t put values in state that can be calculated using existing values, state, and/or props&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Derived state can lead to inconsistencies, especially when the derived data becomes out of sync with the source state. Instead, compute these values on-the-fly using functions or memoization techniques.&lt;/p&gt;

&lt;p&gt;Additionally, always strive to keep state localized as much as possible. Avoid lifting state too high up the component tree unless absolutely necessary. By keeping state close to where it is used, you reduce the complexity of state management and make your components more modular and easier to test.&lt;/p&gt;

&lt;p&gt;Another important aspect is to group related state variables together. When state variables are logically connected, it makes sense to manage them as a single unit, often using objects or arrays. This practice not only helps in organizing your state but also simplifies the process of updating multiple related state variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  State should not be mutated
&lt;/h2&gt;

&lt;p&gt;Mutating state is a no-go area in React as it leads to &lt;strong&gt;unpredictable results&lt;/strong&gt;. Primitives (like numbers, strings, and booleans) are already immutable, but if you are using reference-type values, such as arrays and objects, you should never mutate them. According to the React documentation, we should treat state as if it was &lt;em&gt;immutable&lt;/em&gt;. To change state, we should always use the state updater function, which, in the case of the example below, is the setPerson function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;## Why Not Mutate State?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Unpredictable Results: &lt;br&gt;
Directly mutating state can lead to inconsistencies and bugs, as React might not recognize the changes, leading to unpredictable UI updates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Debugging Difficulties: &lt;br&gt;
Mutable state makes it harder to track changes, complicating the debugging process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React Optimization: &lt;br&gt;
React relies on immutability to optimize re-renders. Mutations can disrupt this optimization, causing performance issues.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Primitives in JavaScript, such as strings, numbers, and booleans, are inherently immutable. Assigning a new value to a state variable containing a primitive type will create a new instance of that value.&lt;br&gt;
Primitives in JavaScript, such as strings, numbers, and booleans, are inherently immutable. Assigning a new value to a state variable containing a primitive type will create a new instance of that value.&lt;br&gt;
Always use the state updater function provided by React to change state. For example, if you're managing a person object, use setPerson to update the state.&lt;/p&gt;

&lt;p&gt;Here is an example:&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%2Fbqrz8vhsxi4hbsye8a8n.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%2Fbqrz8vhsxi4hbsye8a8n.png" alt=" " width="800" height="694"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How state updates
&lt;/h2&gt;

&lt;p&gt;State updates are asynchronous. What this implies is that whenever you call the setState function, React will apply the update in the next component render. This concept can take a while to wrap your head around. With a lot of practice, you’ll get the hang of it in no time.&lt;/p&gt;

&lt;p&gt;Remember, state variables aren’t reactive; the component is. This can be understood by the fact that calling &lt;em&gt;setState&lt;/em&gt; re-renders the entire component instead of just changing the state variable on the fly.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Creating forms in React.</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Fri, 21 Jun 2024 04:41:17 +0000</pubDate>
      <link>https://forem.com/ark7/forms-in-react-38c7</link>
      <guid>https://forem.com/ark7/forms-in-react-38c7</guid>
      <description>&lt;p&gt;Handling &lt;strong&gt;forms&lt;/strong&gt; in React involves managing state, handling user input, and validating the form before submission. &lt;/p&gt;

&lt;p&gt;Contrary to some other ways of dealing with forms on the web, with React, forms are put into a &lt;strong&gt;state variable&lt;/strong&gt;, and then React watches for any form-related event, with every such event updating the form’s state.&lt;/p&gt;

&lt;p&gt;This means that React’s sort of constantly watching your form for any changes.&lt;/p&gt;

&lt;p&gt;We are going to explore how to handle different input forms in react i.e &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Text Inputs&lt;/li&gt;
&lt;li&gt;Textarea&lt;/li&gt;
&lt;li&gt;Checkbox&lt;/li&gt;
&lt;li&gt;Radio Buttons&lt;/li&gt;
&lt;li&gt;Select Dropdown&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We are going to see how is handled and the state functions in react. &lt;br&gt;
To handle forms in React, we are going to break down each step as follows:&lt;/p&gt;

&lt;p&gt;-State Initialization&lt;br&gt;
 -Handling Input Changes&lt;br&gt;
 -Handling Form Submission&lt;br&gt;
 -Form Inputs&lt;br&gt;
 -Form Submission Button&lt;/p&gt;

&lt;p&gt;Lets get into it real quick.&lt;/p&gt;

&lt;p&gt;We are going to start with state initialization.&lt;/p&gt;

&lt;p&gt;We are going to start by handling text inputs. We are going to use firstname, lastname and email as our examples and we go through how to implement them in react forms.&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%2Fyus5n699m6uvc1qpcfys.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%2Fyus5n699m6uvc1qpcfys.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image shown above, we have created a useState that has a firstname, lastname and email inputs.These are text inputs that we are going to discuss as we go along. Let us see how our return statement looks like in our react form file.&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%2F1aaey0gv2fzgsa2em8dw.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%2F1aaey0gv2fzgsa2em8dw.png" alt=" " width="800" height="1013"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above has the types, placeholders and names. This when the app is running, will only render input fields that does not handle any changes on the inputs added and nothing happens when the submit button is clicked.&lt;/p&gt;

&lt;p&gt;Let us go through how we are going to handle change and how we are going to display the values.&lt;/p&gt;

&lt;p&gt;To handle changes in the input fields and display the values when the form is submitted, we need to follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up state management: We'll use state to track the values of the input fields.&lt;/li&gt;
&lt;li&gt;Handle input changes: We'll create a function to update the state whenever an input field value changes.&lt;/li&gt;
&lt;li&gt;Handle form submission: We'll create a function to handle the form submission, which will display the input values.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have already created our state management as shown above. &lt;/p&gt;

&lt;p&gt;Next, we'll create a function to handle changes to the input fields. This function will update the state with the new input values.&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%2F0wzxdhi35ua3hm1pmkxg.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%2F0wzxdhi35ua3hm1pmkxg.png" alt=" " width="800" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have created a function named named &lt;em&gt;handleChange&lt;/em&gt; using an arrow function syntax which can be called whenever an input field value changes.&lt;br&gt;
&lt;em&gt;event&lt;/em&gt; is an object representing the event that triggered the function (in this case, the change event of an input field).&lt;br&gt;
We have used the event target, Lets go through what exactly happens or how we extract the name and the value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;const { name, value } = event.target;&lt;/em&gt; uses object destructuring to  extract the name and value properties from event.target.&lt;br&gt;
 &lt;em&gt;event.target&lt;/em&gt; refers to the HTML element that triggered the event, which is the input field in this case.&lt;br&gt;
 &lt;em&gt;name&lt;/em&gt; corresponds to the name attribute of the input field, and value corresponds to its current value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How do we continue and update the State:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;setFormData({ ...formData, [name]: value });&lt;/em&gt; is the line where the state is updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;setFormData&lt;/em&gt; is a function provided by the useState hook to update the state (formData).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;{ ...formData }&lt;/em&gt; uses the spread operator to create a new object that contains all the properties of the current formData object. This ensures that all existing form data are preserved.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is an important step. Why ?
&lt;/h2&gt;

&lt;p&gt;Because &lt;strong&gt;directly modifying the stat&lt;/strong&gt;e object would &lt;strong&gt;break&lt;/strong&gt; the immutability rule of React state, potentially causing unexpected behavior.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[name]:&lt;/em&gt; value dynamically sets the property of the new object. The square brackets &lt;br&gt;
&lt;em&gt;[name]&lt;/em&gt; is a computed property name in JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This means that "set the property with the key equal to the value of the name variable to the value of the value variable".&lt;br&gt;
For example, if name is "email" and value is "&lt;a href="mailto:example@gmail.com"&gt;example@gmail.com&lt;/a&gt;", it effectively does: email: "&lt;a href="mailto:example@gmail.com"&gt;example@gmail.com&lt;/a&gt;".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Combining the Two:(Using the spread operator as well as dynamic property name).&lt;/p&gt;

&lt;p&gt;The final object looks like &lt;em&gt;{ ...formData, [name]: value }&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This creates a new state object with all the previous properties and updates the specific property (determined by the name attribute of the input field) with the new value.&lt;/p&gt;

&lt;p&gt;Our form has been updated as well so as to be able to handle submit and for its components to have the required fields so that the data filled is submitted when the submit button is clicked. &lt;br&gt;
Each of our input will have an input fild added 'handlechange' that will call the function mentioned above.&lt;br&gt;
Our input field will each have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onChange={handleChange}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Handle Form Submission
&lt;/h2&gt;

&lt;p&gt;We'll create a function to handle the form submission. This function will prevent the default form submission behavior and display the input values.&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%2Fl176pqyinvroguisknfm.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%2Fl176pqyinvroguisknfm.png" alt=" " width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will display the data the user inputs in the field to our console.&lt;br&gt;
We can perform any action after we have got the data from the form. &lt;/p&gt;

&lt;p&gt;We have just handled text inputs, in our next tutorial, we shall cover  Text area and Checkbox in react forms.&lt;/p&gt;

&lt;p&gt;In case of any question or need for clarification, feel free to reach out to me.&lt;/p&gt;

&lt;p&gt;Feel free to follow me on &lt;a href="https://github.com/kibetamos" rel="noopener noreferrer"&gt;Github.&lt;/a&gt; &lt;br&gt;
 Thanks&lt;/p&gt;

&lt;p&gt;Happy coding !&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hooks in React</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 19 Jun 2024 05:47:04 +0000</pubDate>
      <link>https://forem.com/ark7/hooks-in-react-5c8c</link>
      <guid>https://forem.com/ark7/hooks-in-react-5c8c</guid>
      <description>&lt;p&gt;Hooks are functions that let you use React features. All hooks are recognizable by the use prefix. For example, &lt;em&gt;useState&lt;/em&gt; is a hook. &lt;/p&gt;

&lt;p&gt;React Hooks are functions that let you use state and other React features in functional components. &lt;/p&gt;

&lt;p&gt;Introduced in React 16.8, hooks provide a way to manage component logic in a more concise and reusable way compared to class components.&lt;/p&gt;

&lt;p&gt;For now, remember that hooks have rules that we need to abide by:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks can only be called from the top level of a functional component.&lt;br&gt;
   Hooks can’t be called from inside loops or conditions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Commonly Used Hooks
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;useState: Manages state within functional components.&lt;/li&gt;
&lt;li&gt;useEffect: Handles side effects like data fetching, subscriptions, or manually changing the DOM.&lt;/li&gt;
&lt;li&gt;useContext: Provides a way to use React's Context API in functional components.&lt;/li&gt;
&lt;li&gt;useReducer: Manages complex state logic and is an alternative to useState.&lt;/li&gt;
&lt;li&gt;useRef: Accesses DOM elements directly or persists mutable values across renders.&lt;/li&gt;
&lt;li&gt;useMemo: Memoizes expensive calculations.&lt;/li&gt;
&lt;li&gt;useCallback: Memoizes callback functions to prevent unnecessary re-renders.&lt;/li&gt;
&lt;li&gt;useLayoutEffect: Similar to useEffect, but it fires synchronously after all DOM mutations.&lt;/li&gt;
&lt;li&gt;useImperativeHandle: Customizes the instance value that is exposed when using ref in parent components.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well go through afew of the hooks. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;useState&lt;/em&gt;: Manages state within functional components.&lt;/li&gt;
&lt;/ol&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%2Fcwr4vuw63r86pku6mthb.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%2Fcwr4vuw63r86pku6mthb.png" alt=" " width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above; 'useState' initializes state and returns an array with the current state value and a function to update it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;useEffect&lt;/em&gt;: Handles side effects like data fetching, subscriptions, or manually changing the DOM.&lt;/li&gt;
&lt;/ol&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%2Fmd4wjmyveqyktjcm992w.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%2Fmd4wjmyveqyktjcm992w.png" alt=" " width="800" height="421"&gt;&lt;/a&gt;&lt;br&gt;
The example above is a section of the useEffect in a react file when collecting data.Here is how the whole file with the use Effectlooks like.&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%2Fy8kp5nbdy4wwtev53q35.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%2Fy8kp5nbdy4wwtev53q35.png" alt=" " width="800" height="612"&gt;&lt;/a&gt;&lt;br&gt;
These are the most used hooks in react. &lt;/p&gt;

&lt;p&gt;Lets dive and learn more on &lt;em&gt;useState&lt;/em&gt; &lt;a href="https://dev.to/ark7/what-is-state-in-react-4fe9"&gt;&lt;strong&gt;here.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is state in React?</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Sun, 16 Jun 2024 12:35:17 +0000</pubDate>
      <link>https://forem.com/ark7/what-is-state-in-react-4fe9</link>
      <guid>https://forem.com/ark7/what-is-state-in-react-4fe9</guid>
      <description>&lt;p&gt;We write components in React, &lt;strong&gt;A LOT&lt;/strong&gt; of components, and many times we want them to undergo visual changes as a result of user or computer interactions. For this purpose, a component needs to “remember” things about itself. This is where state comes in. &lt;strong&gt;&lt;em&gt;State is a component’s memory.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Any exciting application you build is likely to change over the time the user is exploring it. &lt;/p&gt;

&lt;p&gt;The changes could be as basic as &lt;em&gt;toggling a drop down menu&lt;/em&gt; or as complex as &lt;em&gt;fetching data from an API.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;React provides primitives to manipulate the state of your apps, more specifically components, to make them dynamic. In this lesson, we will learn how to use &lt;strong&gt;state in React&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In React, state is a &lt;em&gt;built-in object&lt;/em&gt; that allows components to create and manage their own data. &lt;/p&gt;

&lt;p&gt;It is an essential concept for building dynamic and interactive user interfaces, enabling components to keep track of information that may change over time and re-render in response to these changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Characteristics of State
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mutable:&lt;/strong&gt;&lt;br&gt;
Unlike props, which are read-only, state is mutable and can be changed.&lt;br&gt;
Changes to state trigger re-renders of the component to reflect the new state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Private&lt;/strong&gt;:&lt;br&gt;
State is local to the component and cannot be directly accessed or                     modified by other components.&lt;br&gt;
It is intended to encapsulate data that is specific to the            component’s functionality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Managed Internally:&lt;/strong&gt;&lt;br&gt;
State is managed within the component itself, often initialized in the constructor (for class components) or using hooks (for functional components).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Understanding the useState Hook in React
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;useState&lt;/em&gt; hook is a fundamental hook in React that allows functional components to manage state. It is used to add state variables to functional components, enabling them to handle dynamic data and trigger re-renders when the state changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax and Usage
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;useState&lt;/em&gt; hook is called inside a functional component and returns an array with two elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&amp;gt; State Variable: The current value of the state.&lt;/li&gt;
&lt;li&gt;&amp;gt; State Setter Function: A function that updates the state and triggers a re-render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the syntax for using the &lt;em&gt;useState&lt;/em&gt; hook:&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%2Faniq9jh7cn22bgxcyh7x.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%2Faniq9jh7cn22bgxcyh7x.png" alt=" " width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;stateValue&lt;/em&gt;: This is the current state value.&lt;br&gt;
&lt;em&gt;setStateValue&lt;/em&gt;: This is the function used to update the state.&lt;br&gt;
&lt;em&gt;initialValue&lt;/em&gt;: This is the initial value of the state, which can be any type (string, number, array, object, etc.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Example: Counter Component
&lt;/h2&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%2Flc2fjq4plo4zq2b1xcpu.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%2Flc2fjq4plo4zq2b1xcpu.png" alt=" " width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this example:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;const [count, setCount] = useState(0); initializes a state variable count with an initial value of 0.&lt;br&gt;
setCount is used to update the count state when the button is clicked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Best Practices of the useState
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initialize State with Proper Default Values:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Ensure you initialize state with meaningful default values to avoid issues during rendering.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Avoid Complex State Logic:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If a state variable requires complex logic or computations to update, consider using the useReducer hook instead of useState.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Use Functions for State Updates Based on Previous State:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;When updating state based on the previous state value, pass a function to the state setter to ensure you get the latest state.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setCount(prevCount =&amp;gt; prevCount + 1);

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Keep State Local and Manageable:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Try to keep state management as local as possible within components. For global state management, consider using state management libraries like Redux or Context API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The useState hook is a powerful and essential tool for managing state in React functional components. By understanding and utilizing useState effectively, you can create dynamic and interactive components that respond to user input and changing data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Knowledge check
&lt;/h2&gt;

&lt;p&gt;The following questions are an opportunity to reflect on key topics in this lesson.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What is state?
What is the useState hook and how would you use it?
What happens to a component when one of its states is changed?
What are some of the rules of hooks?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Mastering React Components</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 12 Jun 2024 13:44:57 +0000</pubDate>
      <link>https://forem.com/ark7/react-components-14eg</link>
      <guid>https://forem.com/ark7/react-components-14eg</guid>
      <description>&lt;p&gt;React components are the fundamental building blocks of a React application. &lt;br&gt;
React components are &lt;strong&gt;reusable&lt;/strong&gt;, &lt;strong&gt;self-contained&lt;/strong&gt; pieces of code that define how a portion of the user interface (UI) should appear and behave. &lt;/p&gt;

&lt;p&gt;The beauty of React is that it allows you to break a UI (User Interface) down into independent reusable chunks, which we will refer to as components. The following picture should give you an idea of how to do that when building a very basic app.&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%2F310x2ibatsbgqkxu2ptk.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%2F310x2ibatsbgqkxu2ptk.png" alt=" " width="800" height="692"&gt;&lt;/a&gt;&lt;br&gt;
For example, this website could be broken into the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;App&lt;/em&gt;, which represents your main application and will be the parent of all other components.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Navbar&lt;/em&gt;, which will be the navigation bar.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;MainArticle&lt;/em&gt;, which will be the component that renders your main content.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;NewsletterForm&lt;/em&gt;, which is a form that lets a user input their email to receive the weekly newsletter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Components can be thought of as custom HTML elements that React uses to build the UI.&lt;br&gt;
React breaks down its components into two types. I.e functional components and class components. &lt;br&gt;
We are going to discuss each one of them one by one. &lt;/p&gt;

&lt;h2&gt;
  
  
  Functional components
&lt;/h2&gt;

&lt;p&gt;Functional components are JavaScript functions that accept props(properties) as arguments and return React elements (JSX). &lt;br&gt;
Functional components do not have their own state or lifecycle methods, but with the introduction of hooks in React 16.8, functional components can now manage state and side effects. &lt;br&gt;
Here is an example of a functional requirement using props. &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%2Fqknl8fro151lsxiqs8gv.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%2Fqknl8fro151lsxiqs8gv.png" alt=" " width="800" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Components:
&lt;/h2&gt;

&lt;p&gt;Class Components are ES6 classes that extend React.Component and must define a render method that returns React elements.&lt;br&gt;
Class components can have their own state and lifecycle methods, making them more powerful than functional components (prior to hooks).&lt;/p&gt;

&lt;p&gt;Here is an example:&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%2Fk3o9zyoqyl6gu2v5c11c.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%2Fk3o9zyoqyl6gu2v5c11c.png" alt=" " width="800" height="664"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  React components key concepts:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. JSX:&lt;/strong&gt;&lt;br&gt;
JSX is a syntax extension for JavaScript that looks similar to HTML and is used to describe the UI.&lt;br&gt;
React components typically return JSX to define what the UI should look like.&lt;br&gt;
Here is a JSX sample:&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%2Fc647lkq3oen8d5ch8jsk.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%2Fc647lkq3oen8d5ch8jsk.png" alt=" " width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Props:&lt;/strong&gt;&lt;br&gt;
Props (short for properties) are read-only inputs to components. They are used to pass data from parent components to child components.&lt;br&gt;
Here is a sample:&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%2Fw774t5tdw7s69km6sc52.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%2Fw774t5tdw7s69km6sc52.png" alt=" " width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. State:&lt;/strong&gt;&lt;br&gt;
State is a built-in object in class components (or managed via hooks in functional components) that holds data that may change over the component's lifetime.&lt;/p&gt;

&lt;p&gt;State changes can trigger re-renders of the component.&lt;br&gt;
Here is an example:&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%2Fk6tp1wtmxoj14wbpl6v0.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%2Fk6tp1wtmxoj14wbpl6v0.png" alt=" " width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This bring us to the end of our discussion on React components.&lt;/p&gt;

&lt;p&gt;You can follow me on GitHub: &lt;a href="https://github.com/kibetamos" rel="noopener noreferrer"&gt;https://github.com/kibetamos&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy hacking!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rendering a list of elements in JSX</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Wed, 05 Jun 2024 07:37:57 +0000</pubDate>
      <link>https://forem.com/ark7/rendering-a-list-of-elements-in-jsx-35am</link>
      <guid>https://forem.com/ark7/rendering-a-list-of-elements-in-jsx-35am</guid>
      <description>&lt;p&gt;Rendering a list of elements in JSX involves iterating over an array of data and generating corresponding JSX elements for each item. &lt;br&gt;
This is typically done using the &lt;em&gt;.map()&lt;/em&gt; function in JavaScript. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's a step-by-step guide:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create the Data Array:&lt;/strong&gt; First, have an array of data that you want to render. This can be an array of objects or simple values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the .map() Function:&lt;/strong&gt; Use the .map() function to iterate over the array and return a JSX element for each item.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Key Prop:&lt;/strong&gt; Each element in the list should have a unique key prop to help React identify which items have changed, are added, or are removed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2Ffqu6xfba4z4gukvuz5yl.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%2Ffqu6xfba4z4gukvuz5yl.png" alt=" " width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the example above:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Array:&lt;/strong&gt; In this example, names is an array of strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSX Rendering:&lt;/strong&gt; In the NameList functional component, the .map() function iterates over the names array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Prop&lt;/strong&gt;: The key prop is set to index in this case. Ideally, use a unique identifier from your data if available.&lt;/p&gt;

&lt;p&gt;Here is another example with an Array of Objects:&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%2F2cuch7pchx05x9dbvqdd.jpg" 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%2F2cuch7pchx05x9dbvqdd.jpg" alt=" " width="800" height="882"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the example above:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Array:&lt;/strong&gt; users is an array of objects, each with an id, name, and age.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSX Rendering:&lt;/strong&gt; In the UserList component, the .map() function iterates over the users array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Prop:&lt;/strong&gt; The key prop is set to user.id, which is a unique identifier for each user object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Considerations:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unique Keys:&lt;/strong&gt; Ensure the key prop is unique among siblings. Using indexes (index) can be problematic if the list is dynamic (e.g., items can be reordered or deleted).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Styling:&lt;/strong&gt; Apply styles as needed to your rendered elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Componentization&lt;/strong&gt;: For more complex list items, consider creating separate components for list items to keep your code modular and maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What challenges are you facing on JSX? Let us know and know how we can help decode. &lt;/p&gt;

&lt;p&gt;We started this series by learning &lt;a href="https://dev.to/ark7/what-is-jsx-5f60"&gt;What is JSX&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this has helped you learn more about JSX. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is JSX?</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Mon, 03 Jun 2024 14:26:37 +0000</pubDate>
      <link>https://forem.com/ark7/what-is-jsx-5f60</link>
      <guid>https://forem.com/ark7/what-is-jsx-5f60</guid>
      <description>&lt;p&gt;JSX, or JavaScript XML, is a syntax extension for JavaScript that allows developers to write HTML-like code within JavaScript. &lt;/p&gt;

&lt;p&gt;It's primarily used with React, a popular JavaScript library for building user interfaces. JSX makes writing React components more concise and readable by combining HTML and JavaScript in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we use JSX?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Declarative Syntax&lt;/strong&gt;: JSX provides a more declarative way to define UI components compared to plain JavaScript. This makes the code easier to understand and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Component-Based Architecture&lt;/strong&gt;: React encourages building applications as a collection of reusable components. JSX makes it easy to define these components in a way that resembles the final UI structure, making the code more intuitive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration of HTML and JavaScript&lt;/strong&gt;: With JSX, you can seamlessly integrate HTML markup and JavaScript logic within the same file, improving code readability and reducing context switching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance Optimization:&lt;/strong&gt; JSX allows React to perform optimizations like Virtual DOM diffing, which helps in efficiently updating the UI without re-rendering the entire DOM tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static Type Checking:&lt;/strong&gt; When used with tools like TypeScript or Flow, JSX enables static type checking, catching errors at compile time rather than runtime, thus enhancing code reliability.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Features of JSX&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HTML-like Syntax&lt;/strong&gt;: Write HTML tags directly in your JavaScript code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JavaScript Expressions&lt;/strong&gt;: Embed JavaScript expressions within curly braces {}.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Component Nesting:&lt;/strong&gt; Easily nest components and manage the structure of your UI.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic Rules of JSX
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Return a Single Root Element&lt;/strong&gt;
Every JSX expression must have a single root element. This means you need to wrap multiple elements in a parent element, like a div, or use React fragments.&lt;/li&gt;
&lt;/ol&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%2Fem1rtrbgdrn37pxyg7cd.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%2Fem1rtrbgdrn37pxyg7cd.png" alt=" " width="800" height="503"&gt;&lt;/a&gt;&lt;br&gt;
As seen above, the return is under one div element. It acts as the root element.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Close all tags.&lt;/strong&gt;
In JSX, all tags must be closed, even the self-closing ones like
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;and&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;input /&amp;gt;.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fs1cw85tz5cvifuuk8rer.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%2Fs1cw85tz5cvifuuk8rer.png" alt=" " width="800" height="348"&gt;&lt;/a&gt;&lt;br&gt;
As seen above the image tag has been enclosed within the div tag. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;camelCase for Attributes&lt;/strong&gt;
Use camelCase for naming attributes instead of the standard HTML attribute names.&lt;/li&gt;
&lt;/ol&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%2Fwh9f56yol7rv9vsgr66o.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%2Fwh9f56yol7rv9vsgr66o.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
As seen in the example, we have use the attribute 'className' as opposed to the normal class used in html syntax. &lt;/p&gt;

&lt;p&gt;Now lets move further and get to know how to render a list of elements in JSX. &lt;a href="https://dev.to/ark7/rendering-a-list-of-elements-in-jsx-35am"&gt;Rendering a list of elements in JSX&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Forms In React</title>
      <dc:creator>M.Ark </dc:creator>
      <pubDate>Tue, 21 May 2024 13:25:50 +0000</pubDate>
      <link>https://forem.com/ark7/forms-in-react-3epl</link>
      <guid>https://forem.com/ark7/forms-in-react-3epl</guid>
      <description>&lt;p&gt;Handling &lt;strong&gt;forms&lt;/strong&gt; in React involves managing state, handling user input, and validating the form before submission. &lt;/p&gt;

&lt;p&gt;Contrary to some other ways of dealing with forms on the web, with React, forms are put into a &lt;strong&gt;state variable&lt;/strong&gt;, and then React watches for any form-related event, with every such event updating the form’s state.&lt;/p&gt;

&lt;p&gt;This means that React’s sort of constantly watching your form for any changes.&lt;/p&gt;

&lt;p&gt;We are going to explore how to handle different input forms in react i.e &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Text Inputs&lt;/li&gt;
&lt;li&gt;Textarea&lt;/li&gt;
&lt;li&gt;Checkbox&lt;/li&gt;
&lt;li&gt;Radio Buttons&lt;/li&gt;
&lt;li&gt;Select Dropdown&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We are going to see how is handled and the state functions in react. &lt;br&gt;
To handle forms in React, we are going to break down each step as follows:&lt;/p&gt;

&lt;p&gt;-State Initialization&lt;br&gt;
 -Handling Input Changes&lt;br&gt;
 -Handling Form Submission&lt;br&gt;
 -Form Inputs&lt;br&gt;
 -Form Submission Button&lt;/p&gt;

&lt;p&gt;Lets get into it real quick.&lt;/p&gt;

&lt;p&gt;We are going to start with state initialization.&lt;/p&gt;

&lt;p&gt;We are going to start by handling text inputs. We are going to use firstname, lastname and email as our examples and we go through how to implement them in react forms.&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%2Fyus5n699m6uvc1qpcfys.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%2Fyus5n699m6uvc1qpcfys.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image shown above, we have created a useState that has a firstname, lastname and email inputs.These are text inputs that we are going to discuss as we go along. Let us see how our return statement looks like in our react form file.&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%2F1aaey0gv2fzgsa2em8dw.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%2F1aaey0gv2fzgsa2em8dw.png" alt=" " width="800" height="1013"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above has the types, placeholders and names. This when the app is running, will only render input fields that does not handle any changes on the inputs added and nothing happens when the submit button is clicked.&lt;/p&gt;

&lt;p&gt;Let us go through how we are going to handle change and how we are going to display the values.&lt;/p&gt;

&lt;p&gt;To handle changes in the input fields and display the values when the form is submitted, we need to follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up state management: We'll use state to track the values of the input fields.&lt;/li&gt;
&lt;li&gt;Handle input changes: We'll create a function to update the state whenever an input field value changes.&lt;/li&gt;
&lt;li&gt;Handle form submission: We'll create a function to handle the form submission, which will display the input values.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have already created our state management as shown above. &lt;/p&gt;

&lt;p&gt;Next, we'll create a function to handle changes to the input fields. This function will update the state with the new input values.&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%2F0wzxdhi35ua3hm1pmkxg.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%2F0wzxdhi35ua3hm1pmkxg.png" alt=" " width="800" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have created a function named named &lt;em&gt;handleChange&lt;/em&gt; using an arrow function syntax which can be called whenever an input field value changes.&lt;br&gt;
&lt;em&gt;event&lt;/em&gt; is an object representing the event that triggered the function (in this case, the change event of an input field).&lt;br&gt;
We have used the event target, Lets go through what exactly happens or how we extract the name and the value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;const { name, value } = event.target;&lt;/em&gt; uses object destructuring to  extract the name and value properties from event.target.&lt;br&gt;
 &lt;em&gt;event.target&lt;/em&gt; refers to the HTML element that triggered the event, which is the input field in this case.&lt;br&gt;
 &lt;em&gt;name&lt;/em&gt; corresponds to the name attribute of the input field, and value corresponds to its current value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How do we continue and update the State:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;setFormData({ ...formData, [name]: value });&lt;/em&gt; is the line where the state is updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;setFormData&lt;/em&gt; is a function provided by the useState hook to update the state (formData).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;{ ...formData }&lt;/em&gt; uses the spread operator to create a new object that contains all the properties of the current formData object. This ensures that all existing form data are preserved.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is an important step. Why ?
&lt;/h2&gt;

&lt;p&gt;Because &lt;strong&gt;directly modifying the stat&lt;/strong&gt;e object would &lt;strong&gt;break&lt;/strong&gt; the immutability rule of React state, potentially causing unexpected behavior.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[name]:&lt;/em&gt; value dynamically sets the property of the new object. The square brackets &lt;br&gt;
&lt;em&gt;[name]&lt;/em&gt; is a computed property name in JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This means that "set the property with the key equal to the value of the name variable to the value of the value variable".&lt;br&gt;
For example, if name is "email" and value is "&lt;a href="mailto:example@gmail.com"&gt;example@gmail.com&lt;/a&gt;", it effectively does: email: "&lt;a href="mailto:example@gmail.com"&gt;example@gmail.com&lt;/a&gt;".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Combining the Two:(Using the spread operator as well as dynamic property name).&lt;/p&gt;

&lt;p&gt;The final object looks like &lt;em&gt;{ ...formData, [name]: value }&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This creates a new state object with all the previous properties and updates the specific property (determined by the name attribute of the input field) with the new value.&lt;/p&gt;

&lt;p&gt;Our form has been updated as well so as to be able to handle submit and for its components to have the required fields so that the data filled is submitted when the submit button is clicked. &lt;br&gt;
Each of our input will have an input fild added 'handlechange' that will call the function mentioned above.&lt;br&gt;
Our input field will each have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onChange={handleChange}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Handle Form Submission
&lt;/h2&gt;

&lt;p&gt;We'll create a function to handle the form submission. This function will prevent the default form submission behavior and display the input values.&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%2Fl176pqyinvroguisknfm.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%2Fl176pqyinvroguisknfm.png" alt=" " width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will display the data the user inputs in the field to our console.&lt;br&gt;
We can perform any action after we have got the data from the form. &lt;/p&gt;

&lt;p&gt;We have just handled text inputs, in our next tutorial, we shall cover  Textarea and Checkbox in react forms.&lt;/p&gt;

&lt;p&gt;In case of any question or need for clarification, feel free to reach out to me.&lt;/p&gt;

&lt;p&gt;Feel free to follow me on &lt;a href="https://github.com/kibetamos" rel="noopener noreferrer"&gt;Github.&lt;/a&gt; &lt;br&gt;
 Thanks&lt;/p&gt;

&lt;p&gt;Happy coding !&lt;/p&gt;

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