<?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: Heba Allah Hashim</title>
    <description>The latest articles on Forem by Heba Allah Hashim (@heba_allah).</description>
    <link>https://forem.com/heba_allah</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%2F3861097%2F934a9d0f-136c-4964-8d1f-d194a68b7def.jpeg</url>
      <title>Forem: Heba Allah Hashim</title>
      <link>https://forem.com/heba_allah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/heba_allah"/>
    <language>en</language>
    <item>
      <title>Annotated in Python and FastAPI: Deep Dive</title>
      <dc:creator>Heba Allah Hashim</dc:creator>
      <pubDate>Sat, 04 Apr 2026 15:03:38 +0000</pubDate>
      <link>https://forem.com/heba_allah/annotated-in-python-and-fastapi-deep-dive-1ba7</link>
      <guid>https://forem.com/heba_allah/annotated-in-python-and-fastapi-deep-dive-1ba7</guid>
      <description>&lt;h2&gt;
  
  
  What is &lt;code&gt;Annotated&lt;/code&gt; in Python?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Annotated&lt;/code&gt; is a feature in Python’s type hints system (introduced in Python 3.9 with PEP 593).&lt;/p&gt;

&lt;p&gt;It allows you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define the type of a variable&lt;/li&gt;
&lt;li&gt;Add extra metadata about how that variable should be handled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python itself ignores this metadata at runtime, but tools (like FastAPI, Pydantic, or linters) can use it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;

&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;must be positive&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;h4&gt;
  
  
  Breakdown:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt;: the core type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"must be positive"&lt;/code&gt;: metadata for tools to interpret&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python itself doesn’t enforce &lt;code&gt;"must be positive"&lt;/code&gt;, but other libraries could use it for validation.&lt;/p&gt;




&lt;h2&gt;
  
  
  FastAPI and &lt;code&gt;Annotated&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;FastAPI uses &lt;code&gt;Annotated&lt;/code&gt; to inject request data (like query params, headers, cookies) into your function parameters.&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Header&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_token_header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;x_token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x_token&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How it works
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x_token&lt;/code&gt;: The parameter name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;str&lt;/code&gt;: The type (must be a string)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Header()&lt;/code&gt;: Metadata telling FastAPI: “Look in the HTTP headers for X-Token”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FastAPI uses this metadata to extract values from the HTTP request automatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why &lt;code&gt;Annotated&lt;/code&gt; matters in FastAPI
&lt;/h2&gt;

&lt;p&gt;It helps FastAPI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separate type definition from request metadata&lt;/li&gt;
&lt;li&gt;Improve readability&lt;/li&gt;
&lt;li&gt;Make dependencies explicit&lt;/li&gt;
&lt;li&gt;Enable powerful dependency injection patterns&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>backenddevelopment</category>
      <category>backend</category>
    </item>
    <item>
      <title>Python Modules and Package</title>
      <dc:creator>Heba Allah Hashim</dc:creator>
      <pubDate>Sat, 04 Apr 2026 14:58:28 +0000</pubDate>
      <link>https://forem.com/heba_allah/python-modules-and-package-2d9k</link>
      <guid>https://forem.com/heba_allah/python-modules-and-package-2d9k</guid>
      <description>&lt;h2&gt;
  
  
  1. Package
&lt;/h2&gt;

&lt;p&gt;A package is a directory (folder) that contains an &lt;code&gt;__init__.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;__init__.py&lt;/code&gt; tells Python:&lt;br&gt;
“This folder is a Python package, so you can import from it.”&lt;/p&gt;
&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myapp/
├── __init__.py
├── helpers.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;myapp&lt;/code&gt; is now a package.&lt;/p&gt;

&lt;p&gt;You can import like this:&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;myapp&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;helpers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Key idea:
&lt;/h4&gt;

&lt;p&gt;A package lets you group related modules into one namespace.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Module
&lt;/h2&gt;

&lt;p&gt;A module is just a single Python file (&lt;code&gt;.py&lt;/code&gt;) that contains Python code.&lt;/p&gt;

&lt;p&gt;You can import it and use its variables, functions, classes, etc.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# utils.py (This is a Python module)
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&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;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can import this in another file:&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alice&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;h4&gt;
  
  
  Key idea:
&lt;/h4&gt;

&lt;p&gt;Any &lt;code&gt;.py&lt;/code&gt; file is a module.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Subpackages and Submodules
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Packages inside packages ⇒ &lt;strong&gt;Subpackages&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Modules inside subpackages ⇒ &lt;strong&gt;Submodules&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example structure:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myapp/
├── __init__.py
├── api/  → is a subpackage
│   ├── __init__.py
│   ├── users.py  → is a submodule
│   └── items.py
├── core.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Key idea:
&lt;/h4&gt;

&lt;p&gt;Subpackages allow you to organize large codebases into smaller logical units.&lt;/p&gt;




&lt;h4&gt;
  
  
  Why so many &lt;code&gt;__init__.py&lt;/code&gt; files?
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;__init__.py&lt;/code&gt; file marks a directory as a package or subpackage.&lt;br&gt;
Without it, Python won’t treat the folder as importable.&lt;/p&gt;

</description>
      <category>python</category>
      <category>backenddevelopment</category>
      <category>softwareengineering</category>
      <category>programming</category>
    </item>
    <item>
      <title>Data Warehouse</title>
      <dc:creator>Heba Allah Hashim</dc:creator>
      <pubDate>Sat, 04 Apr 2026 14:48:07 +0000</pubDate>
      <link>https://forem.com/heba_allah/data-warehouse-1d6f</link>
      <guid>https://forem.com/heba_allah/data-warehouse-1d6f</guid>
      <description>&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A central repository of information that can be analyzed to make more informed decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A data warehouse is specially designed for data analytics, which involves reading large amounts of data to understand relationships and trends across the data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A data warehouse requires that the data be organized in a tabular format.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Architectures
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Source Layer:&lt;/strong&gt; Operational systems, CRM, ERP, and external files (Excel, JSON, SQL) that provide raw data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Staging Area:&lt;/strong&gt; A temporary storage area where data is cleaned, transformed, and integrated before loading into the main warehouse.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storage Layer (Warehouse/Data Marts):&lt;/strong&gt; The central database (RDBMS or Cloud) holding historical and structured data. It may use a Star or Snowflake Schema for organizing data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Access/Analytics Layer:&lt;/strong&gt; Front-end tools for reporting, dashboards, and BI (e.g., SQL tools, Tableau).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Metadata Layer:&lt;/strong&gt; Data that describes the structure, relationships, and context of the stored data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Informed decision making&lt;/li&gt;
&lt;li&gt;Consolidated data from many sources&lt;/li&gt;
&lt;li&gt;Historical data analysis&lt;/li&gt;
&lt;li&gt;Data quality, consistency, and accuracy&lt;/li&gt;
&lt;li&gt;Separation of analytics processing from transactional databases, which improves the performance of both systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ETL (Extract Transform Load)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;&lt;br&gt;
is the process of combining data from multiple sources into a large, central repository called a data warehouse, By applying the process of extract, transform, and load (ETL).&lt;/p&gt;




&lt;h2&gt;
  
  
  OLAP (Online analytical processing)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt;&lt;br&gt;
is a technology used to analyze data stored in a data warehouse by organizing it into multiple dimensions, enabling fast queries and meaningful business insights.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data modeling
&lt;/h2&gt;

&lt;p&gt;Data modeling: is the representation of data in data warehouses or online analytical processing (OLAP) databases as a star or snowflake schema.&lt;/p&gt;




&lt;h2&gt;
  
  
  Operations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Roll up&lt;/li&gt;
&lt;li&gt;Drill down&lt;/li&gt;
&lt;li&gt;Slice&lt;/li&gt;
&lt;li&gt;Dice&lt;/li&gt;
&lt;li&gt;Pivot&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>analytics</category>
      <category>dataengineering</category>
      <category>datawarehouse</category>
      <category>database</category>
    </item>
    <item>
      <title>React.js Refresher</title>
      <dc:creator>Heba Allah Hashim</dc:creator>
      <pubDate>Sat, 04 Apr 2026 14:35:15 +0000</pubDate>
      <link>https://forem.com/heba_allah/reactjs-refresher-hfh</link>
      <guid>https://forem.com/heba_allah/reactjs-refresher-hfh</guid>
      <description>&lt;blockquote&gt;
&lt;h3&gt;
  
  
  💡 &lt;strong&gt;Imperative vs. declarative programming&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The code above is a good example of &lt;strong&gt;imperative&lt;/strong&gt; &lt;strong&gt;programming.&lt;/strong&gt; You're writing the steps for &lt;strong&gt;how&lt;/strong&gt; the user interface should be updated. But when it comes to building user interfaces, a declarative approach is often preferred because it can speed up the development process. Instead of having to write DOM methods, it would be helpful if developers were able to declare &lt;strong&gt;what&lt;/strong&gt; they want to show (in this case, an &lt;code&gt;h1&lt;/code&gt; tag with some text).&lt;/p&gt;

&lt;p&gt;In other words, &lt;strong&gt;imperative programming&lt;/strong&gt; is like giving a chef step-by-step instructions on how to make a pizza. &lt;strong&gt;Declarative programming&lt;/strong&gt; is like ordering a pizza without being concerned about the steps it takes to make the pizza. 🍕&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is JSX?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JSX is a syntax extension for JavaScript, but browsers don't understand JSX out of the box, so you'll need a JavaScript compiler to transform your JSX code into regular JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;React core concepts&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Components&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A component is a function that &lt;strong&gt;returns UI elements&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;React components should be capitalized to distinguish them from plain HTML and JavaScript&lt;/li&gt;
&lt;li&gt;Use React components the same way you'd use regular HTML tags, with angle brackets &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Develop. Preview. Ship.&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;2. Props (properties)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Are inputs &lt;em&gt;read-only information&lt;/em&gt; you pass to a React component to make it dynamic and reusable. And can be passed to components similarly to HTML attributes, but you can pass any JavaScript value through them, including objects and functions.&lt;/li&gt;
&lt;li&gt;Props is an &lt;strong&gt;object&lt;/strong&gt;  &lt;code&gt;{ title: "React" }&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Accessing props
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Dot Notation (&lt;code&gt;props.title&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You write in JSX&lt;/th&gt;
&lt;th&gt;You access in component&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;title="React"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;props.title&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;name="Heba"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;props.name&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Destructuring in Parameters (recommended)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;3. State&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;the logic for updating the state should be kept within the component where state was initially created.&lt;/li&gt;
&lt;li&gt;Every time React re-renders, it &lt;strong&gt;runs the function again from scratch&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;HomePage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Component re-rendered&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// vanilla js&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;normalLikes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleNormalClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;normalLikes&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;normalLikes:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;normalLikes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// using react states&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;stateLikes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setStateLikes&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleStateClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setStateLikes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Normal: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;normalLikes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleNormalClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Normal +1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;State: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;stateLikes&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleStateClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;State +1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Difference
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;let normalLikes = 0&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lives inside the function&lt;/li&gt;
&lt;li&gt;Gets recreated on every render&lt;/li&gt;
&lt;li&gt;Value is lost&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lives inside React&lt;/li&gt;
&lt;li&gt;Value is stored outside the function&lt;/li&gt;
&lt;li&gt;Value persists between renders&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  💡 Mental Model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Render = run function again&lt;/li&gt;
&lt;li&gt;Normal variables = reset every time&lt;/li&gt;
&lt;li&gt;State = saved and restored by React&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  💡 &lt;strong&gt;Events Handling (onClick)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;React event handlers (like &lt;code&gt;onClick&lt;/code&gt;) expect a function, not a function call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incorrect:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Like&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Executes immediately during the render&lt;/li&gt;
&lt;li&gt;Nothing happens on click&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Correct:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Like&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Like&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key Idea:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;onClick={fn}&lt;/code&gt; → pass function (correct)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onClick={fn()}&lt;/code&gt; → execute immediately (wrong)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
  </channel>
</rss>
