<?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: Al-Amin</title>
    <description>The latest articles on DEV Community by Al-Amin (@alamin49).</description>
    <link>https://dev.to/alamin49</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%2F1019467%2Ffbf7041a-fd64-45f6-a8ed-498937309c42.png</url>
      <title>DEV Community: Al-Amin</title>
      <link>https://dev.to/alamin49</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alamin49"/>
    <language>en</language>
    <item>
      <title>Exploring the React component lifecycle: A guide to understanding the different phases.</title>
      <dc:creator>Al-Amin</dc:creator>
      <pubDate>Fri, 29 Nov 2024 20:09:12 +0000</pubDate>
      <link>https://dev.to/alamin49/exploring-the-react-component-lifecycle-a-guide-to-understanding-the-different-phases-1g7g</link>
      <guid>https://dev.to/alamin49/exploring-the-react-component-lifecycle-a-guide-to-understanding-the-different-phases-1g7g</guid>
      <description>&lt;p&gt;এই ব্লগে রিয়েক্ট কম্পোনেন্ট লাইফসাইকেল,  এবং তার বিভিন্ন ফেইস সমপর্কে  উদাহরণ দিয়ে ব্যাখা করবো যাতে বুঝতে সুবিধা হয়।&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;রিয়েক্ট কম্পোনেন্টস&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;লাইফসাইকেলে যাওয়ার আগে,  রিয়েক্ট কম্পোনেন্ট কি তা আগে জেনে নেই।&lt;br&gt;
কম্পোনেন্ট হচ্ছে ইউজার ইন্টরফেসের বিল্ডিং ব্লক। রিয়েক্ট কম্পোনেন্ট &lt;strong&gt;দুই প্রকারের আছে &lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;ফাংশনাল কম্পোনেন্ট &lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ক্লাস কম্পোনেন্ট &lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ফাংশনাল কম্পোনেন্ট অতি সহজ এটা জাভাস্ক্রিপ্ট ফাংশনের মতো।  ফাংশনাল কম্পোনেন্টের বড় সুবিধা হচ্ছে এখানে হুক(Hook) আছে।  যা দিয়ে সহজেই স্টেট ম্যানেজ করা যায় যা ক্লাস কম্পোনেন্ট দিয়ে করা কঠিন&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;কম্পোনেন্ট লাইফসাইকেলের ধাপ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;কম্পোনেন্ট লাফসাইকেল ৩টি মেইন ফেইস দিয়ে গঠিত। &lt;strong&gt;মাউন্টিং,  আপডেটিং, আনমাউন্টিং&lt;/strong&gt;। নিচে এদের ব্যাখা দেয়া হলো&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;মাউন্টিং:&lt;/strong&gt;&lt;br&gt;
Mounting পর্যায়ে React DOM-এ নতুন একটি কম্পোনেন্ট যুক্ত করে। এই ধাপে কয়েকটি গুরুত্বপূর্ণ lifecycle method রয়েছে:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Welcome extends React.Component {

    constructor(props) {

      super(props);

      this.state = { message: "Hello, World!" };

    }

    render() {

      return &amp;lt;h1&amp;gt;{this.state.message}&amp;lt;/h1&amp;gt;;

    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;constructor()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এটি কম্পোনেন্টের প্রথম মেথড।&lt;/li&gt;
&lt;li&gt;state এবং props এর প্রাথমিক মান নির্ধারণে ব্যবহৃত হয়।&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;render()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এটি একটি আবশ্যিক মেথড যা JSX রিটার্ন করে।&lt;/li&gt;
&lt;li&gt;কম্পোনেন্ট কিভাবে DOM-এ প্রদর্শিত হবে, তা নির্ধারণ করে।&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;componentDidMount()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এটি তখনই কল হয়, যখন কম্পোনেন্ট DOM-এ যুক্ত হয়।&lt;/li&gt;
&lt;li&gt;সাধারণত, API কল বা ডেটা ফেচিংয়ের জন্য এটি ব্যবহৃত হয়।
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {

    fetch('https://api.example.com/data')

      .then(response =&amp;gt; response.json())

      .then(data =&amp;gt; this.setState({ data }));

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;আপডেটিং:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shouldComponentUpdate()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এই মেথডের মাধ্যমে নির্ধারণ করা যায়, কম্পোনেন্টটি আপডেট হবে কিনা।&lt;/li&gt;
&lt;li&gt;এটি পারফরম্যান্স উন্নত করতে ব্যবহৃত হয়।&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;render()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;কম্পোনেন্ট আপডেট হওয়া শেষে এটি কল হয়।&lt;/li&gt;
&lt;li&gt;ডেটা ফেচিং বা DOM ম্যানিপুলেশনের জন্য এটি কার্যকর।&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;componentDidUpdate()&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এটি তখনই কল হয়, যখন কম্পোনেন্ট DOM-এ যুক্ত হয়।&lt;/li&gt;
&lt;li&gt;সাধারণত, API কল বা ডেটা ফেচিংয়ের জন্য এটি ব্যবহৃত হয়।
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {

    fetch('https://api.example.com/data')

      .then(response =&amp;gt; response.json())

      .then(data =&amp;gt; this.setState({ data }));

  }

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

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;আনমাউন্টিং:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt; componentWillMount&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;এটি শেষবারের মতো কল হয়, যখন কম্পোনেন্ট DOM থেকে মুছে ফেলা হয়।&lt;/li&gt;
&lt;li&gt;ক্লিনআপ (যেমন, event listener সরানো) করার জন্য এটি ব্যবহৃত হয়।
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillUnmount() {

    clearInterval(this.timerID);

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydvccsltwjjd58y3t0xc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydvccsltwjjd58y3t0xc.png" alt="Image description" width="800" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Unlocking the Power of Map, Filter, and Reduce in JavaScript</title>
      <dc:creator>Al-Amin</dc:creator>
      <pubDate>Mon, 18 Mar 2024 20:44:52 +0000</pubDate>
      <link>https://dev.to/alamin49/mastering-map-filter-and-reduce-2nep</link>
      <guid>https://dev.to/alamin49/mastering-map-filter-and-reduce-2nep</guid>
      <description>&lt;p&gt;Map, filter and reduce are commonly used in Frontend development. However, developers are often confused about their behaviour. Each array method map, filter and reduce returns a new array based on the result of the function. In this article, you will also learn the &lt;strong&gt;power of the reduce method.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;The purpose of the &lt;code&gt;Map()&lt;/code&gt; method in JavaScript is to transform each element of an array using a provided callback function and then return a new array containing the transformed elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers=[2,3,4,5,6,7];

numbers.map(function(element, index, array){
    console.log('element',element, 'index',index,'array', array);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;element 2 index 0 array [ 2, 3, 4, 5, 6, 7 ]
element 3 index 1 array [ 2, 3, 4, 5, 6, 7 ]
element 4 index 2 array [ 2, 3, 4, 5, 6, 7 ]
element 5 index 3 array [ 2, 3, 4, 5, 6, 7 ]
element 6 index 4 array [ 2, 3, 4, 5, 6, 7 ]
element 7 index 5 array [ 2, 3, 4, 5, 6, 7 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Are you afraid of seeing the syntax of the map? Don't worry. I'm here to explain you.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Map takes 3 parameters &lt;strong&gt;elements, index and array&lt;/strong&gt;. However,  you need only &lt;strong&gt;element&lt;/strong&gt; to transform into a new array. Keep in mind that the word transformed. For example, Suppose I have an array containing 5 numbers (1 to 5). I want to double each element, but I could also triple them or perform other transformations. Here is an example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers=[1,2,3,4,5];

const doubleIt= numbers.map((element)=&amp;gt;element*2);
console.log(doubleIt) //[ 2, 4, 6, 8, 10 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the purpose of the array method transform each element of an array and return a new array containing the transformed elements. here numbers transformed in [2,4,6,8,10]. I hope now it is clear. &lt;/p&gt;

&lt;h3&gt;
  
  
  Apply Map
&lt;/h3&gt;

&lt;p&gt;Now is the time to apply a &lt;strong&gt;map&lt;/strong&gt; example for better understanding. Here is an example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const students = [
    {id:1, name: 'Ahmed', age: 19, email: 'ahmed@gmail.com' },
    {id:2, name: 'Rahman', age: 23, email: 'rahman@gmail.com' },
    {id:3, name: 'Bablu', age: 22, email: 'bablu@gmail.com' },
    {id:4, name: 'Jakir', age: 21, email: 'jakir@gmail.com' },
    {id:5, name: 'Mahin', age: 17, email: 'mahin@gmail.com' },
];

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

&lt;/div&gt;



&lt;p&gt;From student array, &lt;strong&gt;you want to display only the ID and email&lt;/strong&gt; of the student. How can you do that?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const studentIdAndEmail = students.map(student =&amp;gt; {
    return { id: student.id, email: student.email };
});
console.log(studentIdAndEmail);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    { id: 1, email: 'ahmed@gmail.com' },
    { id: 2, email: 'rahman@gmail.com' },
    { id: 3, email: 'bablu@gmail.com' },
    { id: 4, email: 'jakir@gmail.com' },
    { id: 5, email: 'mahin@gmail.com' }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Filter
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Filter()&lt;/code&gt; method checks each item in an array based on a condition set by a function. If this condition returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers=[2,3,4,5,6,7];

const filteredNum= numbers.filter((element)=&amp;gt;element&amp;gt;4);
console.log(filteredNum);//[ 5, 6, 7 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember I  mentioned the &lt;code&gt;filter()&lt;/code&gt; method which will check each in an array based on a condition. Pay attention to the word =&lt;strong&gt;Condition&lt;/strong&gt;. In the above, you will see a condition applied if the array element is greater than 4 then it will return a new array. As a result, it returns  a &lt;code&gt;[5,6,7]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Apply Filter
&lt;/h3&gt;

&lt;p&gt;Now is the time to apply a &lt;strong&gt;filter&lt;/strong&gt; example for better understanding. Here is an example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const books = [
    { title: 'The Great Gatsby', genre: 'Fiction', publish: 1925, edition: 2004 },
    { title: 'To Kill a Mockingbird', genre: 'Fiction', publish: 1960, edition: 2006 },
    { title: '1984', genre: 'Science Fiction', publish: 1949, edition: 2003 },
    { title: 'The Catcher in the Rye', genre: 'Fiction', publish: 1951, edition: 2001 },
    { title: 'Pride and Prejudice', genre: 'Romance', publish: 1813, edition: 2005 },
    { title: 'The Hobbit', genre: 'Fantasy', publish: 1937, edition: 2012 },
    { title: 'The Da Vinci Code', genre: 'Thriller', publish: 2003, edition: 2018 },
    { title: 'Harry Potter and the Philosopher\'s Stone', genre: 'Fantasy', publish: 1997, edition: 2014 },
    { title: 'The Lord of the Rings', genre: 'Fantasy', publish: 1954, edition: 2001 },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the books array you want to display a &lt;strong&gt;book whose genre is "Thriller" and that was published after 2000.&lt;/strong&gt; How can you display it?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const filterBookData= books.filter((book)=&amp;gt;{
    return book.genre==='Thriller' &amp;amp;&amp;amp; book.publish&amp;gt;=2000
})
console.log(filterBookData)

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [
 {
    title: 'The Da Vinci Code',
    genre: 'Thriller',
    publish: 2003,
    edition: 2018
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;reduce()&lt;/code&gt; method may seem difficult at first. When you see the tutorials of the reduce method you often come across the same example repeatedly, which is summation. But &lt;strong&gt;Reduce is more powerful than simple summation. it combines all elements of an array into a single value by applying a function repeatedly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Don't worry. Reduce is not that hard the way you think. Suppose you have a piggy bank and a bunch of coins scattered on your table. Each coin has a number written on it representing its value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, you want to find out the total value of all coins you have?&lt;/strong&gt;&lt;br&gt;
So, you start picking up each coin, one by one, and you keep adding its value to the amount you've already collected in your piggy bank. The amount in your piggy bank keeps growing as you add more coins.&lt;/p&gt;

&lt;p&gt;In this story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The piggy bank represents the &lt;strong&gt;accumulator,&lt;/strong&gt; which keeps track of the total value collected so far.&lt;/li&gt;
&lt;li&gt;Each coin represents the &lt;strong&gt;currentValue,&lt;/strong&gt; the individual value being added to the accumulator.&lt;/li&gt;
&lt;li&gt;By the end, the total amount in your piggy bank represents the final result returned by the &lt;code&gt;reduce()&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on this story I will give you a example so, that you will more clear about the topic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const coins = [5, 10, 25, 50, 100]; 

const totalValue = coins.reduce((accumulator, currentValue) =&amp;gt; {

    return accumulator + currentValue;
}, 0); // 0 is the initial value of the accumulator

console.log("Total value of coins:", totalValue); // Output: Total value of coins: 190

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

&lt;/div&gt;



&lt;p&gt;I have told you &lt;code&gt;reduce()&lt;/code&gt; is more powerful than summation. Here is the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const names = [
    'Al-Amin',
    'Abdul',
    'Abu boklor',
    'Bijoy ',
    'Bisso',
    'Emran',
    'Fahad',
    'Fahim',
    'Hamim',
    'HM Rahman',
    'Hridoy Ahmed',
    'Jahid Mia',
    'Johir',
    'Md Al-Amin',
    'Md Rakib',

];

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

&lt;/div&gt;



&lt;p&gt;From this name array I want to display namesGroup like this&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  A: [ 'Al-Amin', 'Abdul', 'Abu boklor' ],
  B: [ 'Bijoy ', 'Bisso' ],
  E: [ 'Emran' ],
  F: [ 'Fahad', 'Fahim' ],
  H: [ 'Hamim', 'HM Rahman', 'Hridoy Ahmed' ],
  J: [ 'Jahid Mia', 'Johir' ],
  M: [ 'Md Al-Amin', 'Md Rakib' ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const namesGroup= names.reduce((acc, curr)=&amp;gt;{
    const firstLetter= curr[0].toUpperCase();
    if(firstLetter in acc){
        acc[firstLetter].push(curr)
    }
    else{
        acc[firstLetter] = [curr];
    }
    return acc;
}, {})
console.log(namesGroup)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;"JavaScript's array methods map, filter, and reduce are powerful tools for manipulating arrays efficiently.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;map() transforms each element of an array according to a given function, returning a new array of the transformed elements.&lt;/li&gt;
&lt;li&gt;filter() selects elements from an array based on a specified condition, returning a new array containing only those elements.&lt;/li&gt;
&lt;li&gt;reduce() applies a function to each element of an array, accumulating a single value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These methods can greatly simplify array manipulation tasks, leading to cleaner and more expressive code. Understanding how and when to use them can significantly improve the efficiency and readability of JavaScript code."&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
