<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Chan on Medium]]></title>
        <description><![CDATA[Stories by Daniel Chan on Medium]]></description>
        <link>https://medium.com/@danchan020?source=rss-2b2f7efa33a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tfYaLE3VEiyWsOshoGMz9A.jpeg</url>
            <title>Stories by Daniel Chan on Medium</title>
            <link>https://medium.com/@danchan020?source=rss-2b2f7efa33a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 16:54:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danchan020/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[the Moment it “Clicks”]]></title>
            <link>https://medium.com/@danchan020/the-moment-it-clicks-e049f97d455a?source=rss-2b2f7efa33a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e049f97d455a</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[codingbootcamp]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[career-change]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Daniel Chan]]></dc:creator>
            <pubDate>Fri, 15 Jul 2022 20:07:03 GMT</pubDate>
            <atom:updated>2022-07-15T20:07:03.990Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Tips and tricks to achieve the euphoric feeling of understanding while learning how to code</em></h4><p>If you are like me, and have made the difficult decision of changing careers into software engineering, I will personally tell you that learning how to code is no easy feat. I am currently approaching the last phase of the software engineering program at Flatiron and it has been anything but easy. Nevertheless, I have gained invaluable skills that will benefit the career switch. Whether you are in the midst of deciding on enrolling in a coding bootcamp, or learning how to code on your own, here are a few tips and tricks to help you get your “click” moment.</p><ol><li><strong>Be comfortable with not understanding</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eCicTJ6WrtN9rXkS.png" /><figcaption>frustrated coder</figcaption></figure><p>Learning any new skill is going to be tough. But learning how to code is like learning a new language. Be prepared to not understand the many different concepts which are introduced in the material. As intensive as it may be, realize that it is okay, and be comfortable with it. Learning takes time and patience. As long as you continue to put in the practice and stay consistent with writing code, all the pieces will eventually all fall together.</p><p><strong>2. Do more than just reading sample code</strong></p><p>As a coder, you WILL refer to other sources of code to learn. Instead of just reading from the source you found, find a way to practice applying it. When I first started learning a concept, I personally found it that I thought I understood a concept while I was reading about it. However, once it came to applying it to a code challenge or project, I suddenly realized I didn’t know as much as I thought. Sometimes code is like that. You will think that a concept isn’t very difficult at all, until it surprises you in a way you have never seen before which really tests your knowledge. To combat this, actually tinker with the code and develop the understanding on what goes on.</p><p><strong>3. Talk to yourself (or a rubber duck), or pseudocode when writing code</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*doNg84PEFCx0gkt_dmN9Ng.jpeg" /><figcaption>cute ducky on desk</figcaption></figure><p>Familiarize yourself with explaining the steps while writing code. Talking to yourself helps you make sure you know what is going on, and further guides you to figure out what you need to do next. There is a technique for coders called the rubber duck technique. When stuck on an issue, the coder should explain the code line by line to the duck or other inanimate object on the desk. Doing so helps coders to get past what we call coders block. This can also be accomplished by pseudo-coding. Pseudocode is a text-based description of the steps required to complete a problem/algorithm. So if you have trouble talking to yourself, or a rubber duck, feel free to just write down your steps! Either of these is better than to only work through a problem in your head while you are stuck.</p><p><strong>4. Understand when you are tired, and take a step back from coding</strong></p><p>Trust me, if you are going to listen to anything I’ve written in this blog, this is the one. A tired brain does not do any favors while learning a new skill, especially coding when it is so technical. When I first started out my bootcamp, I found myself staying up late to complete lab materials, only for them to completely blip out the next day. Your brain needs to be alert in order for it to retain any information. That is why, when you are tired, make sure to take a break. You may find that even those 15 minutes of rest can lead you to figure out a problem you spent too much time stuck on.</p><p><strong>5. ASK for HELP and HELP others!</strong></p><p>Following up on spending too much time being stuck. Give up that pride. You do not want to spend hours being stuck on a piece of code. If you are in a bootcamp, you have instructors or cohort mates you can rely on! In the same way they support you, you should also return the favor. Helping others while learning how to code helps you personally drill down new concepts. More practice is never bad, especially when you will get exposed to different bugs you have never encountered.</p><p><strong>6. Mix up the content you are consuming while learning</strong></p><p>From class modules, web documentation, blogs, and youtube videos, remember the internet is available for you to use! If you are unable to figure things out through one method of learning, there is a world of content out there for you to reference. Sometimes just hearing or seeing a different example is all you need for things to start registering.</p><p>As a coder, you will always be a student because there will always be more to learn. As a student, you will always encounter roadblocks and obstacles in the pursuit of knowledge. I hope that by applying some of these tips and tricks, your coding journey will have less bumps in the road, and that it helps you gain the momentary euphoria after solving each coding problem you run into. Thanks for listening and feel free to share! 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e049f97d455a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Styling with Material UI]]></title>
            <link>https://medium.com/@danchan020/styling-with-material-ui-783749e61219?source=rss-2b2f7efa33a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/783749e61219</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[styling-in-react]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[material-ui]]></category>
            <dc:creator><![CDATA[Daniel Chan]]></dc:creator>
            <pubDate>Fri, 01 Jul 2022 04:29:54 GMT</pubDate>
            <atom:updated>2022-07-01T20:07:36.714Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*irsjLz_oGun-gVR-.png" /></figure><p>Material UI is an open source component library React UI framework that implements Google’s Material Design developed in 2014. It includes many pre-built customizable components which reduces the need for any single developer to create their own. It is pretty neat and saves a lot of time in regards to styling. As someone who first started using MUI recently, I can say it can be a bit overwhelming since it has a lot of information to comb over, but once you get a hang of it, it can be quite helpful in implementing features on your frontend. In this article, I will be going over how to apply some basic components of the MUI library.</p><blockquote>First things first…</blockquote><p>The most important step is installation! To install Material UI, run this command in the terminal:</p><pre>npm install @mui/material @emotion/react @emotion/styled</pre><p>Amazing. With full access, it is time to explore the wonderful components of the MUI library. Let’s start with the most common feature that everyone uses. Guessed it? That’s right:</p><p><strong>Buttons</strong></p><p>A button is an input component which allows users to make an action with a single click. They can be placed practically anywhere to allow user interaction. In your code, simply plug in the &lt;Button&gt; component and add customizable attributes in the opening tag as shown below!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CnM4odck0Kwk0Q6_cxeY8Q.png" /><figcaption>Example taken from MUI website</figcaption></figure><p>Best part about this is that it works almost the same as a button tag in HTML! Just add your onClick handlers the same way you would before and you will have a fully functional button with clean styling.</p><p><strong>Card</strong></p><p>Wondering how to focus some content about a single subject? A card is a surface component which localizes content onto a card-like surface. Check out this example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*dnXZxt1CzDDOg4G2Mt3Izw.png" /><figcaption>Example taken from MUI website</figcaption></figure><p>It displays information about a specific item or subject all within one card! You can even add buttons and other features in the card! To do so, just import the wanted components from the MUI library and nest them within the &lt;Card&gt;&lt;/Card&gt; component! The Card comes in handy when you are looking to display data in a clean and focused way.</p><p><strong>Dialog</strong></p><p>Ever need a pop-up to display important information or to request information? Look no further because the Dialog is a feedback component that provides additional details or actions about an item. The dialog component freezes the surrounding app environment, and focuses on the content within the dialog. The example below is a dialog implementation for a project I had completed in a bootcamp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ruhei6ouRWWYwTbKVXcLWA.png" /><figcaption>Error message Dialog pop-up</figcaption></figure><p>I had implemented this Dialog to pop up when a user tries to make a same reservation on a boardgame which was already reserved by them. The code below shows how the Dialog was added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07uBf5kQ71_cpGZbR0T_iQ.png" /></figure><p>To add a Dialog to your next project, you need to add the &lt;Dialog&gt;&lt;/Dialog&gt; component and set an attribute with a state value that is triggered when an action such as a click, occurs. The example shown is just a text pop-up, but other actions can be added like selecting items, completing a form, etc. Overall, it is a really cool feature to implement!</p><p><strong>Material Icons</strong></p><p>As developers, it’s nice to make the UI feel a bit fun. Using icons can accomplish this goal! Replace a delete button with a trash can or a like button with a heart; these are all small changes that can dramatically change the UX. There are over 2000 available material icons which can be used through MUI. 2000+?!?! Yup, that’s quite a lot. To access the plethora of material icons, we first have to install the npm package by running the command:</p><pre>npm install @mui/icons-material</pre><p>To use any of the material icons, navigate to MUI’s website to see which icons they have available in their library which you want to integrate. For this example, I am showing la basura; that’s right, trash.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L5fJKJCoj0Hivzxm7uCtag.png" /><figcaption>Trash Icon</figcaption></figure><p>It’s as easy as copying the code from the icon you want to select and importing it into the component that you wish to use it in. At this point, just like with any other component, add the component into your code where you want it to go, like so &lt;DeleteSharpIcon/&gt;. It is customizable by adding attributes within the component tag. And that’s how you implement the material icons!</p><p>Since you got all the way here while reading, I’m guessing you’re looking to implement something a little more complex. Let’s explore the:</p><p><strong>Date and Time Picker</strong></p><p>I’ll be honest, it was a bit tricky attempting to first implement the date and time picker. But looking at the gif below, who wouldn’t want to implement this sweet trick.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*05QNnkYXgWJXoryR.gif" /></figure><p>The feature itself is so neat, essentially opening up a dialog for a user to select a specific date on a calendar, and then a specific time on a clock. The example in the gif has the date and time picker nested within a dialog. Let’s take a look at the code beneath the gif:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*239bRpxZQ_i-xLfNIfierg.png" /><figcaption>Date and Time Picker</figcaption></figure><p>The date and time picker itself is all the code within the Localization Provider component. The localization provider is important because it changes the date engine locale that is used to render the pickers; however, default is English(United States).</p><p>To set up the date time picker, first run the following code in the CLI</p><pre>npm install @mui/x-date-pickers</pre><p>Then install one of the four supported date libraries, and set the dateAdapter prop of the LocalizationProvider like the example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NBG8KPpsaZIw1IVevEYYSg.png" /><figcaption>Date time picker setup</figcaption></figure><p>Once you have that complete, just set the value of the &lt;DateTimePicker/&gt; to state and apply a handler to track the changes of each change in the picker. Essentially, MUI does provide most of the code required, so it is really just refactoring the code to your needs.</p><p>These are just a few of the many components that MUI’s library offers. Something I love about Material-UI is how easy it is to navigate and read their documentation. They present plenty of excellent examples in a very clean and clear format. Interactive examples and code samples are available for whatever feature you are looking for! I would highly recommend diving into the official documentation if you are looking to implement MUI in your next project. Good luck!</p><p><a href="https://mui.com/">MUI: The React component library you always wanted</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=783749e61219" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Web 3.0 | The Emergence of Blockchain Technology |]]></title>
            <link>https://medium.com/@danchan020/web-3-0-the-emergence-of-blockchain-technology-d8584e4908cb?source=rss-2b2f7efa33a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8584e4908cb</guid>
            <category><![CDATA[web3]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[intro-to-blockchain]]></category>
            <dc:creator><![CDATA[Daniel Chan]]></dc:creator>
            <pubDate>Fri, 17 Jun 2022 05:54:13 GMT</pubDate>
            <atom:updated>2022-06-17T05:54:13.317Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xn1OzIzPXd7Lm19W.png" /></figure><p>Chances are if you use the World Wide Web, you’ve come across the terms Web3 and blockchain more than once. So what exactly do these terms mean?</p><h3>The World Wide Web (www)</h3><p>Before we get into Web 3.0, let’s talk about the history and origin of the web. The web was originally born in 1989 when a scientist by the name of Tim Berners-Lee developed protocols which allowed information to be shared across the world. In 1990, Lee had developed the first inception of a web server and browser. This was the start and emergence of Web 1.0.</p><blockquote><strong>Web 1.0 : Read-Only</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*28hRgiu_vm0ZJ3Sm.png" /></figure><p>Web 1.0 occurred roughly between 1990–2004. It refers to a time where the majority users were content consumers. Accessing the web kind was kind of like going to the library and reading a book. It consisted of mainly static websites with zero interactions among users, leading it to be known as the read-only web.</p><blockquote><strong>Web 2.0 : Read-Write</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*LKVnZvP2kMrNOulS.png" /></figure><p>Web 2.0 began in 2004 and is still very much the web we use today. The shift towards Web 2.0 is largely due to the emergence of social media platforms. Social media was a big reason for the web’s mass adoption and allowed users to publish their own content. More platforms were created specifically for user to user content. Instead of just reading off a static webpage for info, users were able to interact with web pages and also put their own content out there for others to see. This however, also allowed companies track your views and clicks on a webpage, which created an advertising revenue model for the web.</p><blockquote><strong>Web 3.0 : Read-Write-Own</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*K_oE5r0WeWloXSor.png" /></figure><p>Web 3.0 is a term coined in 2014 by Ethereum co-founder Gavin Wood. As it is still relatively a fairly new idea, many others have their own ideas of what the future of Web 3.0 actually is. However, the overall consensus is that it represents a decentralized online ecosystem based on blockchain technology.</p><p>With blockchain, digital ownership is made possible with the introduction of tokens. Digital assets can be sold or traded, and all verifiable via the blockchain. Present day it can seem kind of silly as people are paying millions of dollars for monkey pictures, but the possibilities of what blockchain tech can be used for is intriguing.</p><p><em>So what exactly is blockchain and decentralization?</em></p><h3>Blockchain and Decentralization</h3><p>Blockchain is a variant of distributed ledger technology (DLT), a decentralized database managed by multiple people. It differs from a traditional database where instead of storing data in tables, rows, or column like a traditional database, blockchain stores data in blocks that are algorithmically and cryptographically appended by hash.</p><p>Decentralization in blockchain refers to the transfer of control and decision making from a centralized entity (government, company, individual) to the entire distributed network. Basically, there is no middle-man dealing with any requests or transactions that an individual tries to make.</p><h3>Pros and Cons</h3><p>There are many solutions to problems that decentralization and blockchain technology achieves. Some of these include:</p><ol><li><strong>Transparency/Trust</strong></li></ol><p>We trust companies, governments, and banks with our information and assets all the time. This can cause issues if these institutions have different agendas. What if we don’t want our data to be sold and used? What if we need some money transaction to be instantaneous? It is well known that banks have wait times and big companies like Google sell your information. Decentralization allows each individual to eliminate the trust required to rely on a third party. History of changes made on the chain are transparent and every change made on the blockchain is verifiable.</p><p>2.<strong> No single point of failure</strong></p><p>A blockchain isn’t controlled by a single entity because blocks of information are stored on and verified by multiple devices across the network. Because of this, a blockchain doesn’t have a single point of failure, meaning there will not be downtime. In contrast, centralized web sites and networks go down all the time.</p><p>3.<strong> Security/Immutability</strong></p><p>Data stored on the blockchain is immutable. Due to blockchains hashing techniques to store each transaction on a block, and its append-only data structure, there is no chance for anyone to modify or alter the data once it is stored. This also increases the integrity of blockchain systems as it is also monitored and maintained by miners who validate transactions 24/7. With this amount of oversight it is basically invulnerable to fraud.</p><p>Some downsides to this new tech are:</p><ol><li><strong>Scalability</strong></li></ol><p>Currently a main issue with blockchain is that it is not very scalable. This means that a network has a limited capability to handle data in a short time. The more traffic a network has, the longer it will take to process and validate a transaction.</p><p>2.<strong> Energy Consuming</strong></p><p>A lot of computing power is required to validate transactions and mine cryptocurrencies, which requires a lot of electricity. With more people adopting blockchain and crypto, more miners join to try to make profits.</p><p>3. <strong>Regulation/Crime</strong></p><p>With blockchains recent emergence, there has not been many rules or regulations set up to keep users accountable or safe. On top of that, everything done on the network is anonymous, which can lead to misuse/scams.</p><p>There is great debate as to whether or not these things are worth incorporating into the new web. Some people don’t see blockchain as the future as there are many downsides in its current state of development, but many people are currently trying to further advance blockchain tech and fix some of the issues which have been mentioned.</p><p>I’m sure most people have only heard of blockchain through the exploring of cryptocurrencies, as this is the most common implementation of blockchain today; however, blockchain introduces many other possibilities and avenues with DeFi, dApps, DAOs, gaming, etc. There’s no denying that this emerging tech has great potential. As we are still in the very early stages of blockchain development, it is exciting to think where it can take us in the next decade.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*MSRQosacEFayq3n_" /><figcaption>Most visited sites annually</figcaption></figure><p>This article just barely scratches the surface of the complexity of blockchain and decentralization, so if you’d like to further get in the details I recommend taking a look at these resources:</p><p><a href="https://towardsdatascience.com/a-deep-dive-into-blockchain-d1eb753fb74c">A Deep Dive into Blockchain</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSSo_EIwHSd4%3Fstart%3D80%26feature%3Doembed%26start%3D80&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSSo_EIwHSd4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FSSo_EIwHSd4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f47a914f228d4403d8a82c5c0558ca9/href">https://medium.com/media/7f47a914f228d4403d8a82c5c0558ca9/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FnHhAEkG1y2U%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnHhAEkG1y2U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FnHhAEkG1y2U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/980730f153e1c9dac7c98d1270d10432/href">https://medium.com/media/980730f153e1c9dac7c98d1270d10432/href</a></iframe><p><a href="https://builtin.com/blockchain">Blockchain: What It Is, How It Works, Why It Matters | Built In</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8584e4908cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Forms : Controlled Components and Abstracting Input Change]]></title>
            <link>https://medium.com/@danchan020/react-forms-controlled-components-and-abstracting-input-change-2054f21043d7?source=rss-2b2f7efa33a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2054f21043d7</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[controlled-component]]></category>
            <category><![CDATA[forms]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[react-forms]]></category>
            <dc:creator><![CDATA[Daniel Chan]]></dc:creator>
            <pubDate>Tue, 31 May 2022 01:28:46 GMT</pubDate>
            <atom:updated>2022-05-31T01:28:46.621Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Forms : Controlled Components and Abstracting Input Change</h3><p><em>Making forms more concise and dynamic</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RHS1RC5EfxkuRgwO" /></figure><h3>Controlled Components</h3><p>In React, form inputs are handled by components. However, in HTML this is handled by the DOM. An important point that is taught while learning about forms on the React framework, is to make them controlled.</p><p><em>What does this mean?</em></p><p>Controlled components are components which are controlled by React in a way where the React component that renders a form also controls what updates within the form on further user input by updating its state.</p><p>Let’s take a look at a basic form I have created to use as an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08vd3ZzuiVAiAd8k7iiatw.png" /></figure><p>Looking at this form, we can point out the multiple different sections of our form. Here we can see that our form has a username input, a password input, a version selector, a newsletter checkbox, and a sign up submission button.</p><p>In order to track the user’s input as they are typing, we need to define the state, add an event handler, and add a value that is set to the current state on <strong>all</strong> sections of our form. Note that the example below only shows the changes for the username section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*UqlQsGNdNzh5nnsQs5Gc3g.png" /><figcaption>Setting state on our Form Component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*mkLsyf1q_FrWirEQsyMjSA.png" /><figcaption>Adding event handler and value</figcaption></figure><p>By including this code, we have a controlled component where the input value is always driven by the React state. This makes our code more dynamic as we can now also pass the value to other elements.</p><p>You might be thinking, what if we have an extremely long form? It must be quite tedious to singularly define state for each input field. And you would be right! That is why there is a method of abstracting our input changes in a less tedious, more concise manner.</p><h3>Abstracting Input Changes</h3><p>Eventually when we have to deal with longer forms, to make things easier for ourselves, there are additional changes that can be implemented that will make our form more dynamic and dry. The first change is to create one state variable where the useState is an object of all the inputs, instead of manually creating a state for each input. Take a look below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*8J0om9A2XyuE7LbM8rhu5Q.png" /><figcaption>Changing our multiple state values into one state object</figcaption></figure><p>For our new state variable, we can see formData is an object which contains all the previous inputs that we had defined in state earlier. The state values can be accessed with object dot notation, and we now have access to use the setter function setFormData if we ever want to update our state.</p><p>After changing all our previous state into one state object, we will also have to update each section’s input value and event handling callback. Again, note that the image below is only an example of the username input field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*mY54l6BJQRec5ukkRrHrlQ.png" /><figcaption>Updating username input form value and event handling callback</figcaption></figure><p>With calling on setFormData in the above example, we can pass in formData with a spread operator, which will be a copy of our previous form data and tell it to access the key of username, and update the value with our user input.</p><p>That’s not all! We can further clean up our code by creating a helper function for all our event handlers. A helper function cleans up our code by extrapolating the code written within our input field to a function outside of the return value of our component function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*IHut3Ew3FUD20PNRwIZ2ig.png" /><figcaption>Creating helper function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*ENKboeyOmHTsvKGgsDIYpw.png" /><figcaption>Updating callback for onChange event handler</figcaption></figure><p>In the helper function, handleChange, a variable key is created to reference the target id. This is done so that when a user types in an input field, our event listener can pinpoint the input field from which the user is typing. This allows our key variable to be dynamic, and able to be read when calling on our setFormData function to locate the value of the specific key. We must pass our key variable within square brackets in order for React to recognize that key is a variable rather than a string.</p><p>And there you have it! A controlled, dry, and dynamic form field.</p><p>React considers it best practice to use controlled forms. I hope that after reading this article, you can also make your react forms more controlled, clean, and concise! If you were still unable to follow along, here are some resources I would recommend which further dives into these concepts:</p><p>React Documentation — <a href="https://reactjs.org/docs/forms.html">https://reactjs.org/docs/forms.html</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FIkMND33x0qQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIkMND33x0qQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FIkMND33x0qQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/15c304789d0597ab37b0dda5123b85fa/href">https://medium.com/media/15c304789d0597ab37b0dda5123b85fa/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2054f21043d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking down the Callback Function]]></title>
            <link>https://medium.com/@danchan020/breaking-down-the-callback-function-141e66aa5894?source=rss-2b2f7efa33a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/141e66aa5894</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[beginner-coding]]></category>
            <category><![CDATA[callback-function]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Daniel Chan]]></dc:creator>
            <pubDate>Thu, 12 May 2022 14:39:06 GMT</pubDate>
            <atom:updated>2022-05-12T14:39:06.841Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NFBHG5LC7kvQ0JPc.png" /></figure><p>I remember learning about callback functions as if it were yesterday. I was confused, overwhelmed, and even frustrated as I first attempted to grasp at how it all worked. If you are here reading this, you are probably in the same boat. Truth is, the callback function is many student’s first tough-to-understand concept while learning how to code.</p><blockquote>Why is that?</blockquote><p>It is because callbacks are one of the first fundamentals of Javascript that break the linear thought process that people are so use to. It introduces a programmer’s method of thinking, a more modular approach. I found the best way to get it to “click” was to walk through it in steps and break it down conceptually, so let’s do that together.</p><h4>What is a callback function?</h4><p>A callback function is a function that is passed as an argument for another function. The “inner function” (the callback) is the function which is passed as the argument to the “outer function”</p><p>Let’s start with some simple examples first distinguishing a normal function from a callback function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC26guQXha3uQnxmzGNyxQ.png" /></figure><p>In this first example we have a normal function which contains a parameter. When invoked below, the argument “Daniel” is passed into the parameter. Callbacks work similarly, where instead of the string argument, we are passing in another function to the parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7Wz9-EOpZgcErSjkCbCiQ.png" /></figure><p>Here in the second example we can see how “theCallback” is a function. This function is passed into the second parameter of the outer function when the “greeting” function is called on line 10. It is important to remember that “theCallback” is being passed as an argument into the parameter “callback”, as this part first confuses most people. The parameter does not require the parentheses () which invokes the function, as is not being invoked at that point, but rather it is just storing the value and being the placeholder for the callback so that it can be used within the higher order function. We can see it is later invoked when it is called at the end of the function.</p><h4>Anonymous Callback Functions</h4><p>Callback functions can also be anonymous functions, that is, they don’t have to be assigned to an identifier. Anonymous functions are often used when a callback is only needed to be used in one place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DtuhKyLVhvR_BtxTuGLq6Q.png" /></figure><p>In the example above, we can see how an inner function is called as the first argument within the “setTimeout” function. This inner function does not have a variable which stores its value for use at a later time.</p><p>We can see in the normal callback situation, the inner function is assigned to the variable “timedAlert” and can then be passed as the first argument to “setTimeout”, and again elsewhere if needed, by simply calling on “timedAlert”. It is best practice to use anonymous functions when we only need a function to run in one place.</p><h4>When are they used?</h4><p>The most common use of callback functions are during iterations and event listeners. But honestly, you will quite literally see them all the time with anything JavaScript.</p><p>Let’s look at some examples below and see if you can spot them.</p><p><strong><em>Iterations</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isTiy9XQEi9zZ_bZgQcRaw.png" /></figure><p>Can you spot where the callback is located in the example above? If you said within the forEach function then you are correct! Using the callback in this situation allows our forEach function to console log an output for each element in our array. The forEach function loops through our array, taking each element one at a time, and passes it as an argument to our callback function! Our callback returns the logged command with each element, in this case, each friend name.</p><p><strong><em>Event Listeners (DOM)</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JgzLbPi4qtyW_Sz1BsGJcQ.png" /></figure><p>An event listener is used to look out for a specific event, such as a click or a keyboard event, and performs one or more actions when it said event happens. With an event listener, callbacks are always passed as the second argument within the function. This callback function runs following the occurrence of the event that which the event listener is looking out for. In our example, our event listener waits for a “click” on a “button”. When clicked, the string ‘button clicked’ will be logged.</p><h4>Why are callback functions important?</h4><p>Now that we have seen a few examples of how callbacks are used, you might have the question, why are they so important?</p><p>Callback functions are important for asynchronous programming. Once we begin throwing all our code together, it is important our code can run without running into bugs and other errors. If you used a function synchronously instead of in a callback, then the function could be called before you get the data that the function needs and it would cause issues that require debugging.</p><p>They are also important in keeping our code clean and easy to read. In some instances you may begin to need to nest functions into functions over and over again. Callback hell shown below is an example of this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jfOjAA2QHOsGd-hO2K3ARw.png" /></figure><p>It should be good practice to avoid deeply nested functions, but if it is necessary, assigning a variable to a callback can clean our code up and makes things more understandable compared to the image above.</p><h4>Congrats, you now know callback functions!</h4><p>I hope this was enough to give a basic understanding of callback functions! Callbacks are an important part of JavaScript and is used in many different ways to clean up and modularize our code. Overall, it is just a fundamental and vital concept to understand while learning JavaScript as it is used in other concepts like iterations and event listeners.</p><p>If you didn’t get it this time around, <strong>that is okay</strong>. Callbacks are a confusing topic, but you will learn it if you give it the time! Be patient with yourself and sometimes a different method of consuming content may help. I will link a good video resource below which has helped me conceptualize callback functions. Good Luck!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fqtfi4-8dj9c%3Fstart%3D9%26feature%3Doembed%26start%3D9&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dqtfi4-8dj9c&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fqtfi4-8dj9c%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7394e32b9860bb86942127c366c397b/href">https://medium.com/media/d7394e32b9860bb86942127c366c397b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=141e66aa5894" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>