<?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 Rafia Khandaker on Medium]]></title>
        <description><![CDATA[Stories by Rafia Khandaker on Medium]]></description>
        <link>https://medium.com/@rafiakhandaker?source=rss-2cc38f52f560------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mvx4XJBDBU_zDNfo</url>
            <title>Stories by Rafia Khandaker on Medium</title>
            <link>https://medium.com/@rafiakhandaker?source=rss-2cc38f52f560------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:26:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rafiakhandaker/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[Routing With useHistory]]></title>
            <link>https://medium.com/@rafiakhandaker/routing-with-usehistory-6d1d6bfa508f?source=rss-2cc38f52f560------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d1d6bfa508f</guid>
            <category><![CDATA[routing]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[client-side-routing]]></category>
            <dc:creator><![CDATA[Rafia Khandaker]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 18:30:45 GMT</pubDate>
            <atom:updated>2022-11-16T18:38:41.717Z</atom:updated>
            <content:encoded><![CDATA[<p>So you’re building a react app and started your client-side routing. How can we redirect the user from page to page? We can utilize the useHistory hook.</p><p>Before we can add the useHistory hook to our React Router, we will need to import it:</p><pre>import { useHistory } from &#39;react-router-dom&#39;;</pre><p>Then we’ll declare it within our component — here as <em>history</em>:</p><pre>const history = useHistory();</pre><p>And now finally, let’s see this hook in action:</p><p>Here we have a signup form — after signup, we want to welcome our user to our site. We’ll put <em>history</em> into the handleSubmit, along with the path that we want the user to be redirected to:</p><pre>  <br>  function handleSubmit(e) {<br>    e.preventDefault();<br>    history.push(&#39;/welcome&#39;); #push the user to a welcome page after signup<br><br>    fetch(&quot;/signup&quot;, {<br>      method: &quot;POST&quot;,<br>      headers: {<br>        &quot;Content-Type&quot;: &quot;application/json&quot;,<br>      },<br>      body: JSON.stringify({<br>        username,<br>        password,<br>        password_confirmation: passwordConfirmation,<br>      }),<br>    }).then((r) =&gt; {<br>      if (r.ok) {<br>        r.json().then((user) =&gt; setUser(user));<br>      }<br>    });<br>  }<br><br><br>  return (<br>        &lt;form onSubmit={handleSubmit}&gt;<br>        &lt;label&gt;Username:<br>        &lt;input<br>          type=&quot;text&quot;<br>          id=&quot;username&quot;<br>          value={username}<br>          onChange={(e) =&gt; setUsername(e.target.value)}<br>        /&gt;&lt;/label&gt;<br><br>        &lt;label&gt;Password:<br>        &lt;input<br>          type=&quot;password&quot;<br>          id=&quot;password&quot;<br>          value={password}<br>          onChange={(e) =&gt; setPassword(e.target.value)}<br>        /&gt;&lt;/label&gt;<br><br>        &lt;label&gt;Confirm Password:<br>        &lt;input<br>          type=&quot;password&quot;<br>          id=&quot;password_confirmation&quot;<br>          value={passwordConfirmation}<br>          onChange={(e) =&gt; setPasswordConfirmation(e.target.value)}<br>        /&gt;&lt;/label&gt;<br>        &lt;button type=&quot;submit&quot;&gt;Sign up now!&lt;/button&gt;<br>      &lt;/form&gt;<br>  );</pre><p>And there you have it! Happy routing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d1d6bfa508f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nesting Data: Active Model Serializers]]></title>
            <link>https://medium.com/@rafiakhandaker/nesting-data-active-model-serializers-525896d62ff3?source=rss-2cc38f52f560------2</link>
            <guid isPermaLink="false">https://medium.com/p/525896d62ff3</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Rafia Khandaker]]></dc:creator>
            <pubDate>Wed, 26 Oct 2022 05:03:08 GMT</pubDate>
            <atom:updated>2022-10-28T04:46:47.599Z</atom:updated>
            <content:encoded><![CDATA[<p>A default Active Model Serializer helps us customize the JSON data rendered from a single model — but we can customize them even further to include associated model data!</p><p>Take a serializer based on Artists:</p><pre>class ArtistSerializer &lt; ActiveModel::Serializer<br>     attributes :id, :name<br>end</pre><p>And a serializer based on Albums:</p><pre>class AlbumSerializer &lt; ActiveModel::Serializer<br>     attributes :id, :title, :year, :artist_id<br>end</pre><p>Let’s say we wanted to show all of the albums that belonged to an artist and go <strong>one nest association deep</strong>. We can update our existing artist serializer with the addition of the association (<em>has_many :albums</em>), <em>or</em> create a new custom one demonstrated below with the association and the attributes we want to display:</p><pre>class ArtistAlbumSerializer &lt; ActiveModel::Serializer<br>     attributes :id, :name<br><strong>     has_many :albums</strong></pre><pre>end</pre><p>This custom serializer can be added into it’s respective controller with the following syntax:</p><pre>def show<br>     artist = Artist.find(params[:id])<br>     render json: artist, status: :ok,<br><strong>     serializer: ArtistAlbumSerializer</strong></pre><pre>end</pre><p><strong>Now what if we wanted to go deeper down the nested associations?</strong> Let’s say an album has many reviews — how can we display all the reviews for an album by an artist? Let’s update our Album serializer first:</p><pre>class AlbumSerializer &lt; ActiveModel::Serializer<br>     attributes :id, :title, :year, :artist_id</pre><pre><strong>     belongs_to :artist<br>     has_many :reviews</strong></pre><pre>end</pre><p>We need to add <em>include</em> to the top-most level of the nest’s controller — in this case, that would be our artist controller:</p><pre>def show<br>     artist = Artist.find(params[:id])<br>     render json: artist, status: :ok,<br><strong>     include: [&#39;albums&#39;, &#39;albums.reviews&#39;]</strong></pre><pre>end</pre><p>Finally we will need to add the macro for this nested level into our custom serializer:</p><pre>class ArtistAlbumSerializer &lt; ActiveModel::Serializer<br>     attributes :id, :name<br><strong>     has_many :reviews</strong></pre><pre>end</pre><p>Happy nesting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=525896d62ff3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby: Rake It Up]]></title>
            <link>https://medium.com/@rafiakhandaker/ruby-rake-it-up-58785bcb0564?source=rss-2cc38f52f560------2</link>
            <guid isPermaLink="false">https://medium.com/p/58785bcb0564</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[rubygems]]></category>
            <dc:creator><![CDATA[Rafia Khandaker]]></dc:creator>
            <pubDate>Wed, 05 Oct 2022 03:29:23 GMT</pubDate>
            <atom:updated>2022-10-05T13:47:23.139Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/899/0*0zGMUR_gXQ1FjUlj.jpg" /></figure><p>Rake is a task-runner written in Ruby that we’d be lost without.</p><p>In order to use this tool, we have to:<br>- ensure the <strong>rake</strong> <strong>gem</strong> is installed on our project.</p><pre>gem install rake</pre><p>- ensure a <strong>Rakefile</strong> is included at the project’s root.</p><p>The format of a task we can add to our Rakefile is task + name of the task + a block that contains the code we want to execute.</p><pre>desc &#39;outputs welcome to the terminal’<br>task :welcome do <br>   puts “Welcome to my Medium blog!” <br>end</pre><p>Now from the terminal, we can run this task by entering</p><pre>rake welcome #outputs Welcome to my Medium blog!</pre><p><em>What if we want to group related tasks? <br></em>We can do so by namespacing multiple tasks:</p><pre>namespace :blog do <br>desc ‘outputs welcome to the terminal’ <br>   task :welcome do <br>      puts “Welcome to my Medium blog!” <br>   end <br>desc ‘outputs reminder to subscribe’ <br>   task :subscribe do <br>      puts “Don’t forget to subscribe!” <br>   end <br>end</pre><p>Now to call a task from the terminal we’ll include the <em>blog</em> heading with the task name we want to output:</p><pre>rake blog:subscribe #outputs Don&#39;t forget to subscribe!</pre><p>Speaking of the terminal, rake is an incredibly useful tool to run and debug tasks with specific parameters within the terminal.</p><p><strong>What tasks can we run with rake?</strong></p><p>Seed a database with data:</p><pre>rake db:seed</pre><p>Migrate a database table:</p><pre>rake db:migrate</pre><p>Start up the Pry console:</p><pre>rake console</pre><p>Rollback a table:</p><pre>rake db:rollback</pre><p>Drop a table:</p><pre>rake db:drop</pre><p>Reset your database:</p><pre>rake db:reset</pre><p>These are just a few of the capabilities at our fingertips through rake!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58785bcb0564" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[useEffect: side effects may include…]]></title>
            <link>https://medium.com/@rafiakhandaker/useeffect-side-effects-may-include-efd47846fb6d?source=rss-2cc38f52f560------2</link>
            <guid isPermaLink="false">https://medium.com/p/efd47846fb6d</guid>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[state]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rafia Khandaker]]></dc:creator>
            <pubDate>Wed, 14 Sep 2022 03:58:12 GMT</pubDate>
            <atom:updated>2022-09-14T14:28:39.903Z</atom:updated>
            <content:encoded><![CDATA[<p>React components are rendered to build out DOM elements, which are the main effects from a component. When we add state to a component, it is re-rendered.</p><p>When we want to run an effect after rendering, we run a side effect with the <strong>useEffect</strong> hook.</p><p><em>What are some instances of side effects?</em></p><p>We’ll utilize useEffect when we’re making network requests, accessing data from a database, writing to the file system, using a timer, and changing the DOM.</p><p>Let’s check out the format of this hook below:</p><pre>import {useEffect } from &#39;react&#39;;</pre><pre>useEffect(() =&gt; {<br><br>});</pre><p>So now we have the basic structure of our useEffect hook, but we need one more addition to our code. If we run it as is, we’ll fall into an endless loop — the side effect is run after every render, which sets the state, which causes a re-render, and then triggers the effect again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OXp5ncKGszTnHJKKinc2MA.png" /></figure><p>In order to stop this loop, we’ll supply an array — known as a <strong><em>dependencies array </em></strong>— as a second argument to the effect hook.</p><pre>import {useEffect } from &#39;react&#39;;</pre><pre>useEffect(() =&gt; {<br><br>}, []);</pre><p>We can use a dependencies array in a few different ways —</p><ul><li>[ ] : an empty array will run the side effect only on the first time our component renders.</li><li>[value1, value2, value3] : an array with a value or values used in the side effect will run the effect after any of these values change.</li></ul><p>This will stop our endless fetch and implement your side effect!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efd47846fb6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: Using forEach() to loop through an array]]></title>
            <link>https://medium.com/@rafiakhandaker/javascript-using-foreach-to-loop-through-an-array-abd43b3a3f19?source=rss-2cc38f52f560------2</link>
            <guid isPermaLink="false">https://medium.com/p/abd43b3a3f19</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[loop]]></category>
            <category><![CDATA[foreach]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <dc:creator><![CDATA[Rafia Khandaker]]></dc:creator>
            <pubDate>Wed, 24 Aug 2022 02:07:34 GMT</pubDate>
            <atom:updated>2022-08-24T02:07:34.959Z</atom:updated>
            <content:encoded><![CDATA[<p>Every programmer has their preferred loop — whether it be fruit loop, forEach() loop, or for loop! Let’s delve into forEach() loops today.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/0*sDZJjqYHEiFad_BS.png" /></figure><p><strong>Introducing the forEach() method:</strong></p><p>The forEach() method allows you to call a function once on every element in an array. This method:</p><ul><li>does not have a built-in return value — it returns undefined</li><li>doesn’t store anything in memory</li><li>works with synchronous functions only</li></ul><p><strong>Syntax &amp; Method Calls:</strong></p><p>The forEach function has three parameters — only one is required:</p><ul><li>Value: (required) The current element that is being processed in our array.</li><li>Index: (optional) The index of the current array element .</li><li>Array: (optional) The full array on which the method is being called upon.</li></ul><p>Let’s see it in action:</p><p>array.forEach(function(value, index, array) {</p><p>console.log(value);</p><p>});</p><p>Let’s say we have an array of numbers that we want to multiply by five:</p><p>const numbers = [65, 44, 12, 4];</p><p>How can we use forEach() to iterate through the array? There are a couple of different ways we can do this. All three examples below will garner the same outputs:</p><p>Anonymous Function Example:</p><p>numbers.forEach(function(number){<br> console.log(number*5)<br>})</p><p>Arrow Function Example:</p><p>numbers.forEach((number) =&gt; {<br> console.log(number*5)<br>})</p><p>Callback Function Example:</p><p>function multiplier(number) {<br> console.log(number*5)<br>}<br>numbers.forEach(multiplier);</p><p><strong>forEach() vs for loop:</strong></p><p>We can also execute the forEach() loops above with a for loop — let’s check out the function equivalent with a for loop:</p><p>for (let i=0; i&lt;numbers.length; i++) {<br> console.log(numbers[i]*5)<br>}</p><p><strong>So when should we use a for loop vs a forEach()?</strong></p><p>If we know how many elements are in the array or the number of iterations we wish to do through an array, we should use a for loop. With forEach(), we don’t need to initialize the index or value variables, since they’re in the arguments of the function. We can also use less code to execute the same result.</p><p>Happy looping!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abd43b3a3f19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>