<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Louiza Mak</title>
    <description>The latest articles on DEV Community by Louiza Mak (@louizamak).</description>
    <link>https://dev.to/louizamak</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%2F1276252%2F9d45fd91-ced7-462b-9a58-37bddc360645.jpeg</url>
      <title>DEV Community: Louiza Mak</title>
      <link>https://dev.to/louizamak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/louizamak"/>
    <language>en</language>
    <item>
      <title>I watched "So You Think You Know Git" so you don't have to</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Wed, 09 Oct 2024 02:13:34 +0000</pubDate>
      <link>https://dev.to/louizamak/i-watched-so-you-think-you-know-git-so-you-dont-have-to-2ak5</link>
      <guid>https://dev.to/louizamak/i-watched-so-you-think-you-know-git-so-you-dont-have-to-2ak5</guid>
      <description>&lt;p&gt;Throughout my coding career so far, I've really only used a few git commands. My most frequent, can be found in the code block below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
git pull
git clone
git add
git commit
git push
git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And on occasion, I'll pull out a &lt;code&gt;git rebase&lt;/code&gt; if I have to do something fancy or to fix a nerve-wracking mistake. So, I've made it my mission to get much more familiar with git and how git branches work when it comes to larger repos this month. It's also my first Hacktober so this is really more for me than it is for you. But if you're here and you're interested in what I have to say, than more power to you. I invite your curiosity and hope you can learn something new or brush up on an old fact lost over the years.&lt;/p&gt;

&lt;p&gt;I recently watched Scott Chacon's &lt;a href="https://www.youtube.com/watch?v=aolI_Rz0ZqY" rel="noopener noreferrer"&gt;FOSDEM 2024&lt;/a&gt; talk, "So You Think You Know Git". He's one of the co-founders of GitHub, co-author of "Pro Git", and co-creator of Git Butler. The video was from 8 months ago, but in case it slipped your radar like it did mine, here are some personal highlights. I definitely encourage anyone reading to watch the full video, but if you're short on time, I've got you covered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditional Configs
&lt;/h2&gt;

&lt;p&gt;A fantastic feature of GitHub that allows you to work on personal or company projects with easy and flexibility. &lt;code&gt;includeIf&lt;/code&gt; will allow you to assign what .gitconfig file is used for specific directories, letting you manage multiple git identities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fr2dsobdu590uycfpclzd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fr2dsobdu590uycfpclzd.png" alt="Two linkedif lines of code" width="510" height="243"&gt;&lt;/a&gt;&lt;/p&gt;
Scott Chacon - FOSDEM 2024



&lt;h2&gt;
  
  
  Oldies But Goodies
&lt;/h2&gt;

&lt;p&gt;Scott covers a few notable time-saving git commands that have been around for quite some time. &lt;/p&gt;

&lt;h3&gt;
  
  
  git maintenance
&lt;/h3&gt;

&lt;p&gt;This command adds a cron job for git to run every hour and perform maintenance tasks on your repository. Since this is running in the background, it speeds up other git commands since they do not have to tack the processes onto other git commands and run as an afterthought. This helps to optimize git repository data.&lt;/p&gt;

&lt;h3&gt;
  
  
  --force-with-lease
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;git push --force-with-lease&lt;/code&gt; is recommended over &lt;code&gt;git push --force&lt;/code&gt; as it is much safer. The command will check if someone has already pushed changes to the branch you're working on and will only allow your push if the branch has not diverged. If the branch has already changed, the push will be rejected which ensures that you will not accidentally overwrite someone else's work. This adds a layer of safety when you're working in collaborative environments. &lt;/p&gt;

&lt;h2&gt;
  
  
  Big Repo Changes
&lt;/h2&gt;

&lt;p&gt;After Microsoft bought GitHub, many implementations were made to benefit monorepos or large repositories. The goal was to make working in with these large repositories faster and more streamlined.&lt;/p&gt;

&lt;h3&gt;
  
  
  prefetching
&lt;/h3&gt;

&lt;p&gt;Similar to &lt;code&gt;git maintenance&lt;/code&gt;, every hour the system will fetch references from the server for the project you're working on. It will not update references until the command &lt;code&gt;git fetch&lt;/code&gt; is ran, but it makes this process faster because the data was already prefetched.)&lt;/p&gt;

&lt;h3&gt;
  
  
  partial cloning
&lt;/h3&gt;

&lt;p&gt;Allows users to filter out blobs, an object type used to store contents of each file in a repo, and only fetch what you need. This makes the repo act almost like a file system, increasing the speed. One con of this feature is that it does not work with &lt;code&gt;git blame&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  commit-graph write
&lt;/h3&gt;

&lt;p&gt;This command caches an index of all the commit objects so that it can do commit graph operations very fast. It is essentially a pre-computed graph that represents a commit history in a more efficient way because the larger a repo, the slower commit graph walks will be. This is because git needs to traverse through each commit to be able to answer any questions such as &lt;code&gt;git log&lt;/code&gt; or &lt;code&gt;git blame&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  GitButler
&lt;/h2&gt;

&lt;p&gt;The last section of Scott's talk was about his current project, &lt;a href="https://gitbutler.com/" rel="noopener noreferrer"&gt;GitButler&lt;/a&gt;, a git client that allows you to work on simultaneous branches. It utilizes a drag and drop feature that quickly organizes file changes to separate virtual branches. There also seems to be intuitive branch management features and could make working on multiple PR requests that much easier.&lt;/p&gt;

&lt;p&gt;I have not personally used it, but I think it's right up my alley and can ensure that I don't make any extra commits to ongoing PR's for my Hacktober. I'll have to give it a try!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fc5n0lnqhwvrtrrxtqow9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fc5n0lnqhwvrtrrxtqow9.png" alt="Scott Chacon at FOSDEM 2024" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;
Scott Chacon - FOSDEM 2024



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Scott covers a few more topics in his video and goes further in depth with these commands, old and new. He also provides some stellar visual examples throughout the PowerPoint presentation. I found his talk entertaining and his "shotgun" style kept topics to the point. It also made me realize there is so much more to learn about Git than I had anticipated. Scott mentioned how there are about 145 git commands, 82 of which is relevant currently. And that is much more than I use in my Git toolbelt.&lt;/p&gt;

&lt;p&gt;I'll have follow-up git-based blogs in the near future as I continue researching and learning more!&lt;/p&gt;

</description>
      <category>git</category>
      <category>learning</category>
      <category>github</category>
    </item>
    <item>
      <title>A deep dive into my project, CORS, and Authorization</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Fri, 20 Sep 2024 19:46:15 +0000</pubDate>
      <link>https://dev.to/louizamak/my-deep-dive-into-my-project-cors-and-authorization-fb7</link>
      <guid>https://dev.to/louizamak/my-deep-dive-into-my-project-cors-and-authorization-fb7</guid>
      <description>&lt;p&gt;Just this week, I finally deployed (for the first time) my most recent project. I call it LifeSense, the humble beginnings of an app that helps you monitor and analyze data from your CGM (Continuous Glucose Monitoring) system. It utilizes OPENAI API to interpret provided data sets and returns a summary of the results and any suggested medical action that should be taken. Of course, this does not substitute a checkup given by a medical professional, but it does offer free and continuous insight as well as trend analysis from people's homes.&lt;/p&gt;

&lt;p&gt;Although parts of the project did provide a challenge, such as working with MUI more deeply to achieve a smooth and dynamic line graph, the biggest challenge I faced was setting up a working sessionStorage. It would allow users to stay logged in even when the page is refreshed. This integral part of my post-deployment took longer than any hurdle so far.&lt;/p&gt;

&lt;h2&gt;
  
  
  CORS
&lt;/h2&gt;

&lt;p&gt;My initial issue was first understanding CORS (Cross-Origin Resource Sharing). I've had console errors in previous projects that were easily fixed, but I never had to communicate between my own backend and frontend before. Because of this project, I feel that I'm a step closer to truly understanding the intricacies of CORS. &lt;/p&gt;

&lt;p&gt;I first started with the basics. What is CORS? Why was it made? Why is it the bane of everyone's existence? Where it all began was the 'same-origin policy' introduced in 1996. Understandably, it's a security function to prevent scripts in other domains from accessing sensitive data. However, in cases like mine, where we need to access an API, CORS steps in. Introduced in 2006, CORS is an extension or workaround to the single-origin policy.&lt;/p&gt;

&lt;p&gt;CORS is a mechanism that allows resource sharing to other domains while blocking any requests made from rogue JavaScript AJAX requests. I learned that it's triggered whenever you are making HTTP requests to anything other than your exact domain. That includes a different domain, sub-domain, protocol (HTTP/HTTPS), and port. This is the exact scenario I encountered when I decided to host my server on an EC2 container and my client on a S3 bucket. My two solutions were to either allow all origins access or to whitelist a specific domain only. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://postimg.cc/m1kkWpZ6" rel="noopener noreferrer"&gt;&lt;img src="https://media.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%2Fv08ssrzh9na8jw93d3mu.png" alt="Assign-Origin.png" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For security and traffic reasons, it was better for my project to only allow my own client access right now but that may change in the future as I continue to develop LifeSense. But since my console was free from errors and the frontend and backend were talking to each other as they should, I moved on to my next hurdle - authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication
&lt;/h2&gt;

&lt;p&gt;A component I implemented for the first time in this project was a login feature that depended on sessionStorage to persist the user session while browsing the app. The sign-up and login functions were simple to accomplish using Formik and Yup for form validation and error handling. Keeping the user logged through a refresh also worked in the development stage. But when I deployed the app, that aspect broke and had to be completely reworked. Here's my train of thought:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What issue am I experiencing?
&lt;/h3&gt;

&lt;p&gt;While browsing the components through the nav bar, my user remains logged in and can access pages that pull data based on the user's ID. Logout and login worked as intended. The issue arose when I was already logged in and then refreshed the browser. The user gets redirected to the home page and tabs that are visible logged in are removed. However, I already had a &lt;code&gt;check_session&lt;/code&gt; Resource in my Flask backend server that checked client-side sessions on refresh. It's supposed to grab the &lt;code&gt;user_id&lt;/code&gt; from the request so there must be a disconnect somewhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Is the &lt;code&gt;user_id&lt;/code&gt; getting passed in the request?
&lt;/h3&gt;

&lt;p&gt;I double-checked and found that the &lt;code&gt;user_id&lt;/code&gt; was  NOT getting passed in the request. Instead, the server received a NoneType, which is why the &lt;code&gt;check_session&lt;/code&gt; failed. This also made sense because now that the front and backend were deployed and no longer hosted on the same machine, I needed to manually store the &lt;code&gt;user_id&lt;/code&gt; where it could be accessed. &lt;br&gt;
I decided to use sessionStorage and assigned it within the &lt;code&gt;promise.then()&lt;/code&gt; after my frontend POST request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://postimg.cc/3yhwxxJG" rel="noopener noreferrer"&gt;&lt;img src="https://media.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%2Fc0360k8a50mgkbl8r89j.png" alt="session-Storage-set-Item.png" width="800" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. How do I pass my sessionStorage data as part of my request?
&lt;/h3&gt;

&lt;p&gt;This is when I learned more about &lt;code&gt;headers&lt;/code&gt; in a request. Previously, I had only used &lt;code&gt;'Content-Type': 'application/json'&lt;/code&gt; which indicates that the format of the request body is in JSON. This time, I focused on the Authentication header which gives the user permission to access a particular resource. As a security measure, users typically aren't permitted to access data from every URL. In most uses of the Authentication header, you would provide an access_token string for the value. However because my project's scope was limited and I only needed to pass a single integer &lt;code&gt;user_id&lt;/code&gt;, I kept it simple for this deployment.&lt;br&gt;
I checked that the header data and Authentication was now included in the request. The backend Resource could now grab it with &lt;code&gt;request.headers.get('Authorization')&lt;/code&gt; and compare it with the database to ensure the user is registered, logged in, and can continue browsing after browser refresh. Huzzah!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As this was my first time deploying a project, CORS and the communication between the frontend and backend were my biggest hurdles. However, after breaking the problem down I was able to research each moving part of CORS and the request-response cycle to come up with the overall solution to these issues. It was a learning experience and through this, I am now more confident in my understanding of CORS and other aspects of the fetch request. I hope this provided insight and help to any other programmer facing the same issues or having trouble debugging and breaking down problems into smaller digestible parts.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>development</category>
      <category>react</category>
      <category>flask</category>
    </item>
    <item>
      <title>Building a RESTful API with Flask</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Mon, 22 Jul 2024 21:07:47 +0000</pubDate>
      <link>https://dev.to/louizamak/building-a-restful-api-with-flask-3091</link>
      <guid>https://dev.to/louizamak/building-a-restful-api-with-flask-3091</guid>
      <description>&lt;p&gt;I'm going to try my hand at doing a basic but comprehensive tutorial this time. After working on my first full stack project, I've realized how much I've enjoyed the backend and creation of an API. It was a super neat experience and I feel that I've learned a lot on what I'm developing and why it has to be done a certain way. So here we go!&lt;/p&gt;

&lt;p&gt;If you're working on a Flask project or just trying to learn the next step after Python basics, you're in the right place. A crucial skill for any web developer, beginner or otherwise, is being able to build a RESTful API. RESTful APIs are interfaces that two computer systems use to communicate through HTTP requests. These requests can perform CRUD - standard database functions like creating, reading, updating, and deleting records. To put it simply, it is a way to access the web in a simple and flexible way. For this blog, we will use Flask, a lightweight web framework for Python that makes it easier to create APIs quickly and efficiently. In addition,  we'll cover the basics of RESTful principles, set up a dev environment, and create API endpoints!&lt;/p&gt;

&lt;h2&gt;
  
  
  RESTful Principles
&lt;/h2&gt;

&lt;p&gt;REST AKA Representational State Transfer is an architectural style for designing networks applications. Key principles of REST include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Statelessness:&lt;/strong&gt; Each request from a client to a server must contain all the information needed to understand and process the request. This way, requests are independent of all previous requests and are stored client side. The API can then be scaled to millions of concurrent users while maintaining simplicity for development, testing, and maintenance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Uniform Interface:&lt;/strong&gt; This interface constraint simplifies and decouples the architecture of the API by standardizing how resources are sent. API should use standard HTTP methods such as GET, POST, PUT, and DELETE. In addition, data sent from the server is typically represented in JSON or XML format. This representation of the data should also include enough information to modify or delete this resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client-Server Architecture:&lt;/strong&gt; Client and server should both be independent and able to change separately. Keep it simple. This is the current standard practice of web dev.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layered System:&lt;/strong&gt; API architecture is composed of layers with their own responsibilities. For example, the API can be deployed on layer A, data is stored on layer B, and authentication requests in layer C. Each of these layers do not know anything about the other servers and this allows load-balancing and improved system availability.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Development Environment
&lt;/h2&gt;

&lt;p&gt;A development environment is a tool that creates an isolated environment for developers to make changes without affecting a live environment. It usually contains its own set of processes and programming tools as well.&lt;/p&gt;

&lt;p&gt;Make sure Python is installed before we start coding. We will also use some other libraries such as &lt;code&gt;pip&lt;/code&gt;, Python package installer, and Flask.&lt;/p&gt;

&lt;p&gt;First, install Flask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install Flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, create a project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir flask_restful_api
$ cd flask_restful_api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, install Flask-RESTful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install Flask-RESTful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  API Endpoints
&lt;/h2&gt;

&lt;p&gt;For this example, we will help out a shelter and make a simple API to manage their current dogs. We will also create endpoints to do CRUD operations. First, let's create a simple project structure. Create these files if you haven't already.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flask_restful_api/
├── app.py
├── models.py
└── config.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, define the Dog model. We will give each dog basic traits to keep track of - name, breed, and age. We will also create an array of three dogs for testing purposes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fdt7n6awrxtqq592aaven.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fdt7n6awrxtqq592aaven.png" alt="Dog model" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Second, we need to create the API Resources. Below is an example of the primary HTTP methods which correspond to the CRUD operations of create, read, update, and delete. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fg3vtesyn5dnc3fyujnqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fg3vtesyn5dnc3fyujnqj.png" alt="Dog Resources" width="800" height="938"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we will initialize the Flask application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F84cwsrmwznopr6fwlcv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F84cwsrmwznopr6fwlcv7.png" alt="App Config" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;For testing our application endpoints, you can use an API testing tool like &lt;a href="https://www.postman.com" rel="noopener noreferrer"&gt;Postman&lt;/a&gt; or &lt;a href="https://insomnia.rest" rel="noopener noreferrer"&gt;Insomnia&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the App
&lt;/h2&gt;

&lt;p&gt;We are finally ready to run the application. You will be able to access it at &lt;code&gt;http://127.0.0.1:5000/&lt;/code&gt;. In your project directory, execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These are the basics of creating a RESTful API with Flask. Using this as a launching off point, you'll be able to build a more robust API and soon connect it to a database and a frontend like React. For future suggestions on what to look into, check out the following below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://react.dev" rel="noopener noreferrer"&gt;REACT&lt;/a&gt;: An open-source frontend JavaScript library. React is great for developing single-page applications and easy to pick-up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sqlalchemy.org" rel="noopener noreferrer"&gt;SQLAlchemy&lt;/a&gt;: Python SQL toolkit and ORM (Object Relational Mapper). Use it to work with databases with Python objects.&lt;/p&gt;

&lt;p&gt;Using React and SQLAlchemy in tandem with Flask-RESTful will allow you to create your first full-stack project with your own API. There are also many other powerful tools similar to the listed above that can be used. Happy exploration and coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>flask</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>My first foray into OOP</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Mon, 20 May 2024 21:35:43 +0000</pubDate>
      <link>https://dev.to/louizamak/my-first-foray-into-oop-4mmd</link>
      <guid>https://dev.to/louizamak/my-first-foray-into-oop-4mmd</guid>
      <description>&lt;p&gt;Okay, maybe it wasn't &lt;em&gt;really&lt;/em&gt; my first foray into OOP, but I don't think I'm going to count the couple pages of "Java: A Beginner's Guide" I read about a year ago. Or the one-off C++ class I ended up dropping after a few months because I was a Biology student and I really thought I had no business being in there. A comp sci minor was just not in the cards at that time. For the most part, I've stuck to my side of the road since I began my coding journey and learned as much JavaScript a beginner could before hopping into a bootcamp. It's where I began building my foundation and learning the fundamentals of what it is to think and breathe like a programmer. It's where I was most comfortable and where I was most confident. I breezed through the lessons, waded through a whole section of React, and learned that React was infinitely better. And, not to toot my own horn, I was pretty decent at it - at least according to my bootcamp requirements. I was feeling great. And then, I was hit with Python.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1xei0259xyhiat95m8lj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1xei0259xyhiat95m8lj.png" alt="Python indentation meme" width="500" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The spaces, the indentations, the lack of curly brackets. I thought I was going to lose my mind. Reading Python was like reading a run-on sentence the length of a paragraph. Although the language syntax bore some resemblance to JavaScript, which I grew to love, I initially struggled to adjust. And after all that adjusting, I was faced with another, larger hurdle. Learning about and using Classes.&lt;/p&gt;

&lt;p&gt;Python is an object-oriented programming language, as I'm sure most readers here already know. But if you're a beginner like me, it's not explicitly clear how this language differs to JavaScript at a glance. Is it because JavaScript &lt;em&gt;doesn't&lt;/em&gt; have objects? Then what was that data type I learned about encompassed in fancy curly brackets? Or is it because Python focuses on this aforementioned data type as its main data structure? Because Python is "object"-oriented, get it? I had a lot of questions and apprehension coming into this new language but I also wanted to keep an open-mind. So here's what I learned for the beginners in the back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects and Classes
&lt;/h2&gt;

&lt;p&gt;A class is a code template to create objects in Python. It's made up of methods and variables and is used to support &lt;em&gt;inheritance&lt;/em&gt; by reducing redundant code. I'll touch on inheritance later on but all you have to know for now is that classes are extremely important in Python because we use them to create objects.&lt;/p&gt;

&lt;p&gt;Everything in Python is an object. And I mean, &lt;em&gt;everything&lt;/em&gt;. Integers, strings, floating point numbers, arrays and even dictionaries, are all objects that are built-in. Objects in Python are instances of a particular class that contain its own data and functions to manipulate that data, almost like its own mini-program. &lt;/p&gt;

&lt;p&gt;To create a class, we use the &lt;code&gt;class&lt;/code&gt; keyword:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F0z2qn15jn975elm4knux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F0z2qn15jn975elm4knux.png" alt="Class keyword code block" width="800" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And to create an object, you call the class:&lt;br&gt;
&lt;a href="https://media.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%2Fofqp90lp8y84ngxd3i9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fofqp90lp8y84ngxd3i9u.png" alt="Object create code block" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Beyond objects and classes, a lot of people will also throw key terms to memorize at you when you first pick up an OOP language. Abstraction, Polymorphism, Encapsulation, and Inheritance. These concepts are vital to understanding why most major software development companies prefer OOP languages. And thankfully, the couple pages of "Java: A Beginner's Guide" that I read summarized what each term means in the scope of programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction
&lt;/h2&gt;

&lt;p&gt;Goal: Simplifying Complexity&lt;/p&gt;

&lt;p&gt;Abstraction is the process of hiding the complex implementation details of a system and exposing only the necessary parts. It’s like driving a car; you don’t need to understand how the engine works to drive it. You only interact with the steering wheel, pedals, and dashboard.&lt;/p&gt;

&lt;p&gt;In programming, abstraction allows you to define complex operations in simple terms. For example, let's take this car analogy further and consider a Car class in a program:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fhlgvn0o6j7gkem3actw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhlgvn0o6j7gkem3actw7.png" alt="Car class abstraction code block" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;start_engine&lt;/code&gt;, &lt;code&gt;drive&lt;/code&gt;, and &lt;code&gt;stop_engine&lt;/code&gt; are high-level methods that hide the intricate details of how a car starts, drives, and stops. This abstraction makes the Car class easier to use without requiring the user to understand its internal workings as users will only need to call the class along with its method. For example: &lt;code&gt;Car.start_engine()&lt;/code&gt; and &lt;code&gt;Car.stop_engine()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Goal: Reusing Code&lt;/p&gt;

&lt;p&gt;Inheritance allows a class to inherit properties and methods from another class. This promotes reusing code and establishes a natural hierarchy between classes.&lt;/p&gt;

&lt;p&gt;To continue with the car analogy: imagine you have a base class Vehicle and you want to create a Car class and a Bike class. Instead of rewriting common functionality, you can inherit them from Vehicle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F5zkhhx4tizohciou6stj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F5zkhhx4tizohciou6stj.png" alt="Car class inheritance code block" width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, Car and Bike inherit the &lt;code&gt;start&lt;/code&gt; method and other properties from Vehicle, making the codebase more maintainable and DRY (Don't Repeat Yourself). In addition, we can utilize the &lt;code&gt;super&lt;/code&gt; method to avoid even more redundancy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F4ef8xrm9u5for4iqxulu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4ef8xrm9u5for4iqxulu.png" alt="Super method code block" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;super&lt;/code&gt; method returns a temporary object of the parent class and it's typically used to initialize parent class attributes or invoke parent class methods. In the example above, it is used to call the &lt;code&gt;__init__&lt;/code&gt; method from the Vehicle class to make sure the &lt;code&gt;make&lt;/code&gt; and &lt;code&gt;model&lt;/code&gt; attributes are properly initialized without redundant code. This also allows the Vehicle class's &lt;code&gt;__init__&lt;/code&gt; method to be modified without needing to also update its dependent classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&gt;

&lt;p&gt;Goal: Protecting Data&lt;/p&gt;

&lt;p&gt;Encapsulation is all about bundling the data and methods that operate on the data into a single unit, i.e., a class, and restricting access to some of the object's components. This protects the integrity of the data and prevents unintended interference.&lt;/p&gt;

&lt;p&gt;Let's use a new analogy and imagine a Store class. We could create many classes that inherit properties and methods from it but there is one class's data that we want to ensure the integrity of - the CashRegister Class. In the CashRegister class, we want to restrict direct access to the total balance:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fp3hnd7i7y5gidlxftzf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fp3hnd7i7y5gidlxftzf7.png" alt="Cash register code block" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above, &lt;code&gt;__balance&lt;/code&gt; is a private attribute that can only be accessed with the class. The following methods &lt;code&gt;deposit&lt;/code&gt;, &lt;code&gt;withdraw&lt;/code&gt;, and &lt;code&gt;get_balance&lt;/code&gt; provide controlled access to update and view the balance. This ensures the register's data integrity and prevents unintended interference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;Goal: Code Flexibility&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fx7beduccovxkzuliq3mw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fx7beduccovxkzuliq3mw.jpeg" alt="Polymorphism meme" width="500" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Polymorphism allows objects of different classes to be treated as objects of a common super class, something we've touched on throughout this blog. It enables one interface to be used for a general class of actions, making the code more flexible and easier to extend.&lt;/p&gt;

&lt;p&gt;For example, consider an app that processes payments from different sources such as credit cards, PayPal, and Bitcoin. We can define a common interface in a superclass and implement specific details in the subclasses:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fi0kmjzp5al1lkgxuett3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fi0kmjzp5al1lkgxuett3.png" alt="Payment code block" width="775" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;code&gt;process_payment&lt;/code&gt; is defined in the superclass Payment and implemented in each subclass. The &lt;code&gt;process_transaction&lt;/code&gt; function can process any payment method, demonstrating polymorphism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding objects and classes is vital to writing efficient, reusable, and simplified code. It is part of the core foundations needed to excel in Python and understanding other OOP languages. The principles of Abstraction, Inheritance, Encapsulation, and Polymorphism are equally essential for creating robust, flexible, and maintainable software. They enable developers to write code that is not only easier to understand but also easier to modify and extend. By mastering these concepts, even if you have to struggle through it like I did, you can leverage the full power of Object-Oriented Programming to build sophisticated and scalable applications. And at the end of the day, isn't that what we all want to achieve? I know this isn't a thick, well-researched, and well-written book like "Java: A Beginner's Guide", but I hope that this can help you in beginning your own Python journey.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>The Beginner's Guide to React's useEffect Hook</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Wed, 10 Apr 2024 17:48:53 +0000</pubDate>
      <link>https://dev.to/louizamak/the-beginners-guide-to-reacts-useeffect-hook-5djl</link>
      <guid>https://dev.to/louizamak/the-beginners-guide-to-reacts-useeffect-hook-5djl</guid>
      <description>&lt;p&gt;One of the core fundamentals beginner coders learn is that functions are "self-contained" blocks of reusable code that perform a single task. Functions "take in" data, processes it, and "return" a result, and can be run multiple times. But what if they could do more? What if they could have a side effect? Let me introduce React's &lt;code&gt;useEffect&lt;/code&gt; hook. &lt;/p&gt;

&lt;h2&gt;
  
  
  Hooks
&lt;/h2&gt;

&lt;p&gt;If you're new to React, like I was a month and a half ago, you might not be too familiar with hooks, yet. Hooks lets you add stateful logic to a functional component without writing a class. There are several types of hooks built into React including, but not limited to: Effect, Context, and State. &lt;code&gt;useEffect&lt;/code&gt; falls under the Effect hook, hooks that allow components to connect and synchronize with external systems such as the browser DOM, network, and other non-React code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fe53m5uy0fyklirh3tb5h.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fe53m5uy0fyklirh3tb5h.jpeg" alt="Hook meme." width="554" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But why would we need to do that? In the realm of React development, managing side effects is an essential aspect of building robust and efficient applications. Here are a few examples for you. What if we have an object that we want to use to control a non-React component based on the object's State? Or what if we need to perform a GET request right after the page is fully rendered? How about setting up a timer that only increments while it is active? Or simply being able to subscribe to events or update the DOM? These scenarios, crucial for creating dynamic user interfaces, are easily achievable with the &lt;code&gt;useEffect&lt;/code&gt; hook. &lt;/p&gt;

&lt;h2&gt;
  
  
  useEffect Basic Usage
&lt;/h2&gt;

&lt;p&gt;Before we begin, &lt;strong&gt;don't&lt;/strong&gt; forget to import the &lt;code&gt;useEffect&lt;/code&gt; hook! &lt;/p&gt;

&lt;p&gt;Now, let's start with the basics. The &lt;code&gt;useEffect&lt;/code&gt; hook accepts two parameters: a callback function and an array of dependencies. The function is the side effect we want to perform, and the dependencies array allows us to specify values that, when changed, will trigger the effect to run again. This mechanism ensures precise control over when the side effect should be executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1bt9s9mgo9gd66at4p83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1bt9s9mgo9gd66at4p83.png" alt="Basic template for the useEffect hook within a React component." width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is why understanding how dependencies work in &lt;code&gt;useEffect&lt;/code&gt; is key for writing efficient and bug-free code. When we provide a dependency array, React will re-run the effect whenever any of the dependencies change. If you omit the dependency array, the effect will run on every render, potentially causing performance issues. Imagine the side effect code running in an infinite loop, an unstoppable force littering your console with lines of GET request arrays or "Hello, World!". Scary. That's why, at the minimum, we should always supply the &lt;code&gt;useEffect&lt;/code&gt; hook with an empty dependency array. We can also provide arrays that aren't empty,  and these will ensure that the effect will only run when the contents of the array change. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F97q2bk44phrbd1grns37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F97q2bk44phrbd1grns37.png" alt="Basic template of useEffect hook showing off the dependencies array." width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we provide multiple arrays, such as &lt;code&gt;dependency1&lt;/code&gt; and &lt;code&gt;dependency2&lt;/code&gt;, the effect will read this as run when &lt;code&gt;dependency1&lt;/code&gt; &lt;strong&gt;or&lt;/strong&gt; &lt;code&gt;dependency2&lt;/code&gt; changes. And these arrays usually change when we &lt;code&gt;setState&lt;/code&gt; of a component and it re-renders. By default &lt;code&gt;useEffect&lt;/code&gt; will run the side effect function in the following order:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fakklmzcmm1rz3wov3ht9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fakklmzcmm1rz3wov3ht9.png" alt="useEffect hook flow chart." width="800" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By following these dependency array rules, we can optimize performance and avoid unnecessary computations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cleaning Up
&lt;/h2&gt;

&lt;p&gt;Another neat feature of the &lt;code&gt;useEffect&lt;/code&gt; hook is that it is designed to allow the 'return' of a function within it as a means to tidy up our code before the component unmounts. This function also runs right before the execution of the next effect. This is because specific side effects might require cleanup to prevent memory leaks or unwanted behavior. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fwltkkektx934ssalgwi6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fwltkkektx934ssalgwi6.jpg" alt="Clean up meme" width="236" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, let's say you're performing a fetch request on the server to get user A's information but change your mind before the request is complete. Instead, you decide to fetch the information of user B. Without the cleanup function, both fetch requests would continue to run even though the components have been unmounted or the dependencies have changed. This can lead to unexpected behaviors such as the effect attempting to update components that are no longer mounted or display the wrong information. With the cleanup function, we can abort the first fetch request of user A before moving on to user B and ensure that resources are properly released when they're no longer needed.&lt;/p&gt;

&lt;p&gt;The following is another example with corresponding code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ffk298jfqbt4znw3ga0we.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ffk298jfqbt4znw3ga0we.png" alt="use-Effect-Cleanup-Code.png" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;useEffect&lt;/code&gt; hook sets up an interval that increments &lt;code&gt;count&lt;/code&gt; every second. The clean-up function returned by &lt;code&gt;useEffect&lt;/code&gt; clears the interval using `clearInterval(intervalID) which stops the interval from running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, the &lt;code&gt;useEffect&lt;/code&gt; hook is an incredibly essential tool for managing side effects in React functional components. It's versatile and simple and can handle a wide range of scenarios such as fetching data, subscribing to events, and interacting with the DOM. Although we shouldn't use this effect to handle every aspect of our code like orchestrating the data flow of our apps, it is a great hook that lets us "step out" of our React code and synchronize with external systems like API's, databases, networks, and so on. So go ahead, leverage the power of &lt;code&gt;useEffect&lt;/code&gt; for your projects, and embrace this wonderful React hook.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is 'this'?</title>
      <dc:creator>Louiza Mak</dc:creator>
      <pubDate>Fri, 09 Feb 2024 23:26:17 +0000</pubDate>
      <link>https://dev.to/louizamak/what-is-this-f1g</link>
      <guid>https://dev.to/louizamak/what-is-this-f1g</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; was the first JavaScript topic that really had me stumped. I couldn’t understand it no matter how hard I tried, and it was even more difficult trying to figure out how to use it. Don’t even get me started on &lt;code&gt;.call()&lt;/code&gt;, &lt;code&gt;.bind()&lt;/code&gt;, and &lt;code&gt;.apply()&lt;/code&gt;. That’s why I decided to tackle &lt;code&gt;this&lt;/code&gt; for my first coding blog post.&lt;/p&gt;

&lt;p&gt;Let's be honest, context matters. Why? Because without context, we would never be able to make educated decisions, add insightful comments to a conversation, or simply, understand sarcasm. Context is what gives meaning to everything that we do in our daily lives. Context is everything. Context is king. And context is what you need to understand how to use &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Context
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Frk032khapdmuwbwvqk1m.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Frk032khapdmuwbwvqk1m.jpeg" alt="Execution Context" width="800" height="400"&gt;&lt;/a&gt;Image by &lt;a href="https://www.atatus.com/blog/javascript-execution-context/" rel="noopener noreferrer"&gt;Atatus&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution context is the abstract concept of an environment that the JavaScript engine sets aside to handle the transformation and execution of code. In JS, there are two types of execution context: Global Execution Context (GEC) and Function Execution Context (FEC). Embed these acronyms in your brain now, because we will be using them often. &lt;/p&gt;

&lt;p&gt;To start, we need to understand that regardless of which context is being created, both undergo a two-phase creation process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; The Creation Phase&lt;/li&gt;
&lt;li&gt; The Execution Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For GEC, the creation phase begins when an execution context is created but before the code runs. For FEC, the creation phase begins whenever the function is called. Something to keep in mind is that there can be more than one FEC, distinct to each function, but there can only be one GEC. In addition, the Function Execution Context can access the entire code of the Global Execution Context, but not the other way around. This is analogous to how a one-way mirror works, in that, the FEC can “see” externally towards the GEC, but the GEC cannot “see” internally towards the FEC. &lt;/p&gt;

&lt;p&gt;Once the creation phase initiates, a variable object is created that stores the variables and function declarations within the execution context. This object is the Execution Context Object that we will refer to later.&lt;/p&gt;

&lt;p&gt;After, the scope chain is formed. But what is the scope chain and how does it work? It’s all about positioning. We’ll use the analogy again, the one-way mirror. Code within nested functions can “see” externally towards the global scope, but the global scope cannot “see” into functions. We call this lexical scoping. When JavaScript resolves code, the engine will traverse up the scope chain to determine what additional variables and functions the code can access. Keep in mind that scope can not jump laterally, so to speak, so function A does not have the same scope as function B if they are not nested in any way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6QXjyVYX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://blog.hubspot.com/hs-fs/hubfs/JavaScript_Scope-1.webp%3Fwidth%3D650%26height%3D450%26name%3DJavaScript_Scope-1.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6QXjyVYX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://blog.hubspot.com/hs-fs/hubfs/JavaScript_Scope-1.webp%3Fwidth%3D650%26height%3D450%26name%3DJavaScript_Scope-1.webp" alt="JavaScript Scope Chain" width="650" height="450"&gt;&lt;/a&gt;Image by &lt;a href="https://blog.hubspot.com/website/javascript-scope" rel="noopener noreferrer"&gt;Hubspot&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, the value of &lt;code&gt;this&lt;/code&gt; is initialized by the JS engine. The keyword points to an object that the current code is being executed on. What is this object, you may wonder? For GEC and FEC, it is the Execution Context Object that was created at the beginning of the creation phase. This global object is also referred to as the &lt;code&gt;window object&lt;/code&gt; and represents an open window in a browser. Another important way &lt;code&gt;this&lt;/code&gt; can be utilized is in an object method. When used in a method, &lt;code&gt;this&lt;/code&gt; refers to the owner object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fxhcabgffulltmahpmth0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fxhcabgffulltmahpmth0.png" alt="Value of this" width="421" height="319"&gt;&lt;/a&gt;Image by &lt;a href="https://www.scaler.com/topics/javascript/this-keyword-javascript/" rel="noopener noreferrer"&gt;Scalar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that the creation phase is finished, the JS engine starts reading the code and executing it one line at a time. This phase involves assigning values to variables, executing functions and code blocks as it is encountered, and managing the call stack, a data structure that keeps track of functions currently being executed.&lt;/p&gt;

&lt;p&gt;And voila, we have gone over the context needed to finally discuss how to use &lt;code&gt;this&lt;/code&gt;. A round of applause for everyone who’s made it this far.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s start this out by reiterating that &lt;code&gt;this&lt;/code&gt; is a keyword that refers to an object based on how it is invoked. Within the GEC and FEC, &lt;code&gt;this&lt;/code&gt; refers to the &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F0dvpr0p20siii88bhoxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F0dvpr0p20siii88bhoxf.png" alt="GEC this value" width="358" height="167"&gt;&lt;/a&gt;GEC &lt;code&gt;this&lt;/code&gt; value&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F9lbk9mrvqzkbc8epnx8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F9lbk9mrvqzkbc8epnx8p.png" alt="FEC this value" width="493" height="204"&gt;&lt;/a&gt;FEC &lt;code&gt;this&lt;/code&gt; value&lt;/p&gt;

&lt;p&gt;And within an object method, &lt;code&gt;this&lt;/code&gt; refers to the object itself. How is this possible? We need to understand how object methods are created. Let's look at the following example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fyaovyzyb46kd1t7f4qpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fyaovyzyb46kd1t7f4qpv.png" alt="Object Method Long" width="544" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I've written out a method for &lt;code&gt;food&lt;/code&gt; which prints a string containing &lt;code&gt;fruit&lt;/code&gt;'s value. We can rewrite this 'shorthand'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fdr47dso669c2z9xzhd51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fdr47dso669c2z9xzhd51.png" alt="Object Method Short" width="544" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This shorthand syntax increases readability and saves time. Below, I've taken the code and applied &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ft76ze8p3nc6pzcopxv5c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ft76ze8p3nc6pzcopxv5c.png" alt="Object Method with This" width="544" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice in all three of these scenarios, &lt;code&gt;food.printFruit()&lt;/code&gt; prints the same thing. So, let's circle back to the original question. How is it possible that, within an object method, &lt;code&gt;this&lt;/code&gt; refers to the object itself? It is possible because the moment we invoke the method on the adjacent object &lt;code&gt;food&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; is implicitly bound to &lt;code&gt;food&lt;/code&gt;. I suppose the whole idea is a bit circular but I hope that clears it up!&lt;/p&gt;

&lt;p&gt;Now we know that &lt;code&gt;this&lt;/code&gt; is a keyword that refers to an object based on where and how it is invoked at the time it is invoked. &lt;code&gt;this&lt;/code&gt; is not a variable and we cannot change its value.&lt;/p&gt;

&lt;p&gt;There's so much more to &lt;code&gt;this&lt;/code&gt; that I did not cover in this blog, such as how to use &lt;code&gt;this&lt;/code&gt; when it comes to methods: &lt;code&gt;.call()&lt;/code&gt;, &lt;code&gt;.bind()&lt;/code&gt;, and &lt;code&gt;.apply()&lt;/code&gt;. But that will have to be a post of its own. For now, we have answered the first question. What is &lt;code&gt;this&lt;/code&gt;?&lt;/p&gt;

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