<?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 Irmak Esin on Medium]]></title>
        <description><![CDATA[Stories by Irmak Esin on Medium]]></description>
        <link>https://medium.com/@irmakesin?source=rss-a175972bdf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VC_uxnH-_TcaO88IvDsuow@2x.jpeg</url>
            <title>Stories by Irmak Esin on Medium</title>
            <link>https://medium.com/@irmakesin?source=rss-a175972bdf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 19:13:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@irmakesin/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[Real-Life Front-End Challenges: Mid-Level Interview Questions]]></title>
            <link>https://irmakesin.medium.com/real-life-front-end-challenges-mid-level-interview-questions-0bfa97325a6d?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bfa97325a6d</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[routing]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Mon, 20 Jan 2025 12:09:21 GMT</pubDate>
            <atom:updated>2025-01-21T14:40:04.802Z</atom:updated>
            <content:encoded><![CDATA[<p>This set of interview questions is designed for mid-level front-end developers, focusing on real-world scenarios encountered in actual projects. The aim is to evaluate candidates’ <strong>problem-solving abilities, hands-on experience, and their understanding of core concepts</strong> like <strong><em>React</em></strong><em>, </em><strong><em>JavaScript</em></strong><em>, </em><strong><em>state management</em></strong><em>, </em><strong><em>performance optimization</em></strong><em>, </em><strong><em>API integration</em></strong><em>, and </em><strong><em>modern front-end best practices</em></strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTDv67rJiBv3QARRobSavw.png" /></figure><p>Each question reflects practical challenges, encouraging candidates to think critically and demonstrate their ability to implement effective solutions in complex, real-life applications.</p><ol><li><strong>Performance Problem While Loading Large Data<br><em>Scenario (Question):</em> </strong>Based on a scenario where a project sends a very large JSON dataset to the client at once, leading to extremely long initial page load times, how would you approach optimizing data loading to improve performance and user experience?</li></ol><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Lazy loading</strong>: Instead of fetching all data at once, retrieve data in parts when needed (e.g., when the user scrolls or interacts). This reduces initial load time.</li><li><strong>Pagination</strong>: Splitting large data sets into pages and only loading the relevant page can improve both performance and user experience.</li><li><strong>Code splitting</strong>: In larger applications, avoid bundling all modules for the initial load. Load them dynamically as the user navigates (reduces initial bundle size).</li></ul><p><strong>2. Real-Time Form Submission Causing Performance Issues<br><em>Scenario (Question):</em></strong><em> </em>In an e-commerce application, a multi-field form sends a request to the backend on every keystroke. This overloads the server, increasing response times, and causes lags in the user interface. How would you optimize this?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Apply Debounce or Throttle:</strong> Reduce unnecessary requests by waiting for a specific period after keystrokes (<strong><em>debounce</em></strong>) or sending requests at defined intervals (<strong><em>throttle</em></strong>).</li><li><strong>Batch Updates:</strong> Combine multiple input changes into a single request to the server.</li><li><strong>Local Validation:</strong> Perform as much validation as possible on the client side before sending requests to the server.</li><li><strong>Asynchronous Optimization:</strong> Use optimistic updates to keep the UI responsive and handle server requests in the background if necessary.</li></ul><p><strong>3. Slow Page Loads Due to Large Image Files<br><em>Scenario (Question):</em> </strong>We have high-resolution product images on our site, which significantly slow down page load times. Some users leave before images fully load. How can we tackle this?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Lazy loading (images)</strong>: Only load images when they’re about to enter the viewport, improving initial load times.</li><li><strong>Image Optimization</strong>: Using more efficient formats (WebP, AVIF) and compressing files can greatly reduce image size.</li><li><strong>Responsive Images</strong>: Serve different image sizes or formats based on device or screen size for optimal performance and clarity.</li></ul><p><strong>4. Complex Data Sharing Across Components<br><em>Scenario (Question):</em> </strong>In a React project, prop drilling has become excessive. We pass data through multiple intermediate components, making the codebase hard to maintain. How would you approach this?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Context API</strong>: Provides a way to pass data directly to relevant components without manually drilling through props at every level.</li><li><strong>State Management Libraries</strong>: For more complex data or larger projects, a single source of truth (e.g., Redux store) can be more organized.</li><li><strong>Local vs. Global</strong>: Not all state should be global. Use local state where possible, and only put necessary shared data in context or global state.</li></ul><p><strong>5. Re-render Issue<br><em>Scenario (Question):</em> </strong>In your React application, you’re passing a function as a prop to a child component. Because this function is recreated on every render, the child component re-renders frequently. How can you prevent this?</p><p><strong><em>Explanation</em>:</strong></p><ul><li>Wrap the function in <strong>useCallback</strong> so that it retains the same reference unless its dependencies change.</li><li>This prevents unnecessary re-renders of the child component.</li><li>Alternatively, defining the function outside the parent component or reducing inline creation can also help.</li></ul><p><strong>6. Avoiding Style Conflicts with Global CSS in Large Applications<br><em>Scenario (Question):</em> </strong>Your project uses a <strong>global.css</strong> file, but as new features are added, existing global styles start to affect some components. How would you prevent such style conflicts when working with global CSS?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Utility-First Approach:</strong> Adopt a utility-first approach (e.g., TailwindCSS) to minimize reliance on global CSS and focus on component-specific styles.</li><li><strong>CSS Namespace:</strong> Use a consistent naming convention (e.g., <strong>.app-header, </strong><strong>.app-footer</strong>) to ensure global styles do not unintentionally affect unrelated components.</li><li><strong>Avoid Overly Specific Selectors:</strong> In global CSS, avoid overly specific selectors (<strong>div p span</strong>) and stick to minimal and clear rules to reduce conflicts.</li></ul><p><strong>7. Writing a Custom Hook<br><em>Scenario (Question):</em> </strong>You need to reuse the same logic across multiple components. How would you create a custom hook for this, and what considerations should you keep in mind?</p><p><strong><em>Explanation</em>:</strong></p><ul><li>Consolidate repetitive functionality in a function prefixed with<strong> </strong><strong>use</strong> (e.g., <strong>useFetchData</strong>).</li><li>Manage state, effects, and any cleanup inside the hook.</li><li>Carefully design the hook’s dependency array, returned values, and side effects to ensure predictable behavior.</li></ul><p><strong>8. Event Bubbling &amp; Capture<br><em>Scenario (Question):</em> </strong>Clicking a form element triggers a parent-level event handler, causing unwanted behavior. How do you control this situation?</p><p><strong><em>Explanation</em>:</strong></p><ul><li>Use <strong>e.stopPropagation()</strong> to halt event bubbling.</li><li>Understand the capturing and bubbling phases to intervene at the correct point.</li><li>Sometimes <strong>e.preventDefault()</strong> is also needed, for example, to stop default form submission.</li></ul><p><strong>9. Memory Leaks in React<br><em>Scenario (Question):</em> </strong>You notice that some event listeners or intervals continue running even after a React component has unmounted, causing memory leaks. How can you prevent this?</p><p><strong><em>Explanation</em>:</strong></p><ul><li>Use the cleanup function (return) in <strong>useEffect</strong> to remove event listeners or clear intervals.</li><li>Perform all necessary cleanup during component unmount.</li><li>If using third-party libraries, be sure to call their “<strong><em>destroy</em></strong>” or “<strong><em>unsubscribe</em></strong>” methods as needed.</li></ul><p><strong>10. SSR vs. CSR<br><em>Scenario (Question):</em> </strong>How do you decide between SSR (e.g., Next.js) and a classic CSR approach for a React project? What are the main criteria?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>SEO</strong>: SSR can provide immediate content for search engines to index.</li><li><strong>Performance</strong>: SSR often improves initial paint time on slow devices.</li><li><strong>Project Complexity</strong>: SSR adds extra server and configuration overhead, so for simpler projects, CSR might suffice.</li></ul><p><strong>11. Debounce vs. Throttle<br><em>Scenario (Question):</em> </strong>While typing into an input field, an API call is triggered on every keystroke. You want to improve performance using debounce or throttle. Which one do you choose and when?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Debounce</strong>: Fires the call only after the user has stopped typing for a set duration (useful for search inputs).</li><li><strong>Throttle</strong>: Fires the call at fixed intervals (e.g., once per second), useful for continuously updating metrics like window resizing.</li><li>The choice depends on whether you need to prioritize accuracy (debounce) or speed (throttle).</li></ul><p><strong>12. Handling Side Effects in Redux<br><em>Scenario (Question):</em> </strong>When updating data in a Redux store, how do you handle async requests or other side effects? What approaches do you use?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Redux Thunk</strong>: A good starting point for simple scenarios, allowing actions to be async functions.</li><li><strong>Redux Saga</strong>: Powerful for more complex flows, cancellations, and parallel requests.</li><li><strong>Redux Toolkit</strong>: Has built-in support for thunk, while still allowing advanced middleware like Saga if needed.</li></ul><p><strong>13. Managing Increasingly Complex State and Data Consistency<br><em>Scenario (Question):</em> </strong>Your front-end application has grown significantly, and the amount of shared state across pages has increased. Managing local state (using React hooks like <strong><em>useState</em></strong> or <strong><em>useReducer</em></strong>) has become overly complex, and inconsistencies in data are starting to appear. How would you address this issue to make state management more sustainable and ensure data consistency?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Adopt Global State Management:</strong> Use tools like Context API for lightweight state sharing or Redux for larger applications where state is deeply nested or shared across many components.</li><li><strong>Normalize the State Structure:</strong> Store data in a flat structure to make updates predictable and avoid deeply nested state trees.</li><li><strong>Server State Synchronization:</strong> For data fetched from APIs, use tools like TanStack Query to manage server-side data caching and synchronization. This avoids inconsistencies caused by stale data.</li><li><strong>Implement Modular State Logic:</strong> Divide state into slices or contexts to manage specific features independently while maintaining global access where needed</li></ul><p><strong>14. Keeping User Data Consistent Across Pages<br><em>Scenario (Question):</em> </strong>In your application, users can edit their profile information (e.g., name, age, address) on a profile page, and this data is also displayed on a list page. Users expect their changes to immediately reflect everywhere. How would you manage and synchronize this data across multiple components and pages to ensure consistency?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Centralize Data Storage:</strong> Use a global state management tool like Context API or Redux to store user data. This ensures updates in one part of the app automatically reflect elsewhere.</li><li><strong>Optimistic Updates:</strong> Update the state locally immediately after user actions to make the UI responsive. Synchronize with the server in the background and revert changes if the server update fails.</li><li><strong>Invalidation and Refetching:</strong> For data displayed in lists or dynamically loaded pages, invalidate and refetch the server data after updates to ensure consistency.</li><li><strong>Modular State Sharing:</strong> If certain components require frequent updates, pass data via props or context to avoid unnecessary re-renders of unrelated components.</li></ul><p><strong>15. Managing User Sessions and Data Persistence<br><em>Scenario (Question):</em> </strong>You need to store a user’s session information (e.g., a token) in the browser to keep them logged in when they refresh the page or reopen the application. How would you implement this while balancing security and performance considerations?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Use Local or Session Storage:</strong> Store tokens in localStorage for persistence across browser sessions or sessionStorage for single-session storage.</li><li><strong>HttpOnly Cookies:</strong> For sensitive data like tokens, prefer storing them in HttpOnly cookies to reduce exposure to XSS attacks.</li><li><strong>Token Expiry Handling:</strong> Include token expiry information and refresh tokens to manage session validity without requiring frequent logins.</li><li><strong>Secure Communication:</strong> Always use HTTPS for token transmission and ensure secure cookie flags are set to protect against attacks.</li><li><strong>Revalidation Mechanisms:</strong> Implement a token revalidation mechanism on app initialization to check if the session is still valid and update it if necessary.</li></ul><p><strong>16. Routing with Filters Persisting Across Pages<br><em>Scenario (Question):</em> </strong>On a page, users select filters (e.g., date range and category). After selecting the filters, they navigate to another page. However, the filters are reset on the new page, and users expect their selections to persist. How would you solve this issue?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>Query Parameters:</strong> Append the selected filters as query parameters during navigation. Read and apply these parameters on the new page to update the UI.</li><li><strong>Global State Management:</strong> Use global state management (e.g., <strong><em>Context API or Redux</em></strong>) to store filters so that they remain accessible across pages.</li><li><strong>Persistent State:</strong> Save the filter selections in localStorage or sessionStorage to maintain them even during navigation or page reloads.</li></ul><p><strong>17. Implementing a Theme System Using Global CSS<br><em>Scenario (Question):</em> </strong>In an application, you want users to switch between light and dark themes. The themes should be applied globally across all pages. How would you implement this feature using global CSS?</p><p><strong><em>Explanation</em>:</strong></p><ul><li><strong>CSS Variables for Themes:</strong> Define all theme-dependent styles (e.g., <strong><em>colors, typography</em></strong>) as CSS variables, and update their values based on the selected theme.</li><li><strong>Theme Control via </strong><strong>html or </strong><strong>body:</strong> Use a class on the <strong>html</strong> or <strong>body</strong> tag (e.g., <strong>class=&quot;dark-theme&quot;</strong>) to toggle between themes and apply the respective styles.</li><li><strong>Persist User Preference:</strong> Save the user’s theme selection in <strong>localStorage</strong> and apply it when the app reloads to maintain consistency.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0bfa97325a6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mid-Level JavaScript Interview Questions-Solutions (About Arrays)]]></title>
            <link>https://irmakesin.medium.com/mid-level-javascript-interview-questions-solutions-about-arrays-632297098ab8?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/632297098ab8</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[arraylist]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[array-methods]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Wed, 13 Sep 2023 15:11:21 GMT</pubDate>
            <atom:updated>2024-09-18T10:09:16.350Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve shown 9 questions related to arrays in JavaScript that I’ve encountered in mid-level interviews and beyond, along with their answers. I hope this article proves to be helpful to you as you navigate through these common array-related challenges in your JavaScript interviews.</p><p><strong>1- Print unique values from an array</strong></p><pre>const arr = [1, 2, 3, 3, 4, 5, 5];<br>const uniqueArr = [...new Set(arr)];<br>console.log(uniqueArr);  // [1, 2, 3, 4, 5]</pre><p><strong>2- How do you use the </strong><strong>reduce() method to sum elements in an array without using a loop?</strong></p><pre>const arr = [1, 2, 3, 4, 5];<br>const sum = arr.reduce((acc, current) =&gt; acc + current, 0);<br>console.log(sum);  // 15</pre><p><strong>3- How can you flatten a nested array into a single flat array?</strong></p><pre>const arr = [1, [2, 3], [4, [5, 6]]];<br>const flatArr = arr.flat(Infinity);<br>console.log(flatArr);  // [1, 2, 3, 4, 5, 6]</pre><p><strong>4- How can you find the intersection of two arrays?</strong></p><pre>const arr1 = [1, 2, 3, 4];<br>const arr2 = [3, 4, 5, 6];<br>const intersection = arr1.filter(item =&gt; arr2.includes(item));<br>console.log(intersection);  // [3, 4]</pre><p><strong>5- Write a function that swaps the first two elements in a given array (solve with destructing)</strong></p><pre>function swapFirstTwo([first, second, ...rest]) {<br>    return [second, first, ...rest];<br>}<br><br>const array = [3, 5, 1, 4, 2];<br>console.log(swapFirstTwo(array));  // [5, 3, 1, 4, 2]</pre><p><strong>6- Explain different ways to reverse an array and show each with a code example</strong></p><pre>// Reversing Array Elements Method 1: Using the reverse() method<br>const arr1 = [1, 2, 3, 4, 5];<br>const reversedArr1 = arr1.reverse();<br>console.log(reversedArr1); // [5, 4, 3, 2, 1]<br><br>// Reversing Array Elements Method 2: Using the spread operator and reverse()<br>const arr2 = [1, 2, 3, 4, 5];<br>const reversedArr2 = [...arr2].reverse();<br>console.log(reversedArr2); // [5, 4, 3, 2, 1]</pre><p><strong>7- Explain different ways to sort an array in descending order and show each with a code example.</strong></p><p><strong>Method 1: Using </strong><strong>sort() with a Compare Function</strong></p><p>The sort() method can take a compare function that defines the sort order.</p><pre>const arr = [3, 1, 4, 2, 5];<br>arr.sort((a, b) =&gt; b - a);<br>console.log(arr); // Output: [5, 4, 3, 2, 1]</pre><ul><li>The compare function (a, b) =&gt; b - a sorts the array in descending numerical order.</li><li>This method sorts the array <strong>in place</strong>, modifying the original array.</li></ul><p><strong>Method 2: Using Spread Operator and </strong><strong>sort()</strong></p><p>To avoid mutating the original array, you can create a copy using the spread operator and then sort it.</p><pre>const arr = [3, 1, 4, 2, 5];<br>const sortedArr = [...arr].sort((a, b) =&gt; b - a);<br>console.log(sortedArr); // Output: [5, 4, 3, 2, 1]<br>console.log(arr);       // Original array remains unchanged</pre><ul><li>[...arr] creates a shallow copy of the original array.</li><li>Sorting the copied array does not affect the original array.</li></ul><p><strong>Method 3: Using </strong><strong>slice() and </strong><strong>sort()</strong></p><p>Another way to copy the array before sorting is using the slice() method.</p><pre>const arr = [3, 1, 4, 2, 5];<br>const sortedArr = arr.slice().sort((a, b) =&gt; b - a);<br>console.log(sortedArr); // Output: [5, 4, 3, 2, 1]<br>console.log(arr);       // Original array remains unchanged</pre><ul><li>arr.slice() creates a shallow copy of the array.</li><li>The copied array is then sorted without modifying the original array.</li></ul><p><strong>Method 4: Using </strong><strong>Array.from() and </strong><strong>sort()</strong></p><p>You can also use Array.from() to create a copy before sorting.</p><pre>const arr = [3, 1, 4, 2, 5];<br>const sortedArr = Array.from(arr).sort((a, b) =&gt; b - a);<br>console.log(sortedArr); // Output: [5, 4, 3, 2, 1]<br>console.log(arr);       // Original array remains unchanged</pre><p><strong>8- How do you use the </strong><strong>filter() method to filter out odd numbers from an array?</strong></p><pre>const numbers = [1, 2, 3, 4, 5, 6, 7, 8];<br>const evenNumbers = numbers.filter(num =&gt; num % 2 === 0);<br>console.log(evenNumbers); // [2, 4, 6, 8]</pre><p><strong>9- (Bonus) - Can you copy an array in different ways, and which method is the safest for preventing unintended modifications to the original array?</strong></p><blockquote><strong>First solution:</strong> Using the <strong>spread operator (</strong><strong>...)</strong>. This method creates a new array by spreading the elements of the original array.</blockquote><pre>const originalArray = [1, 2, 3, 4, 5];<br>const copiedArray = [...originalArray];<br>console.log(copiedArray); // [1, 2, 3, 4, 5]</pre><blockquote><strong>Second Solution:</strong> Using the <strong>Array.from()</strong> method. This method creates a new, shallow-copied array instance from an array-like or iterable object.</blockquote><pre>const originalArray = [1, 2, 3, 4, 5];<br>const copiedArray = Array.from(originalArray);<br>console.log(copiedArray); // [1, 2, 3, 4, 5]</pre><blockquote><strong>Third Solution</strong>: Using the <strong>slice()</strong> method. This method returns a shallow copy of a portion of an array into a new array object.</blockquote><pre>const originalArray = [1, 2, 3, 4, 5];<br>const copiedArray = originalArray.slice();<br>console.log(copiedArray); // [1, 2, 3, 4, 5]</pre><blockquote><strong>Safest Method</strong></blockquote><p>All three methods above create <strong>shallow copies</strong> of the original array. This means that for arrays containing only primitive values (like numbers, strings, booleans), these methods are sufficient to prevent unintended modifications to the original array.</p><p>However, if the array contains <strong>nested objects or arrays</strong> (reference types), changes to those nested structures in the copied array will also affect the original array. To prevent this, you need to create a <strong>deep copy</strong> of the array.</p><p><strong>Deep Copy Methods:</strong></p><p><strong>1</strong>- <strong>Using </strong><strong>structuredClone() (Modern Browsers and Environments):</strong></p><pre>const originalArray = [1, 2, { a: 3 }, [4, 5]];<br>const copiedArray = structuredClone(originalArray);<br><br>copiedArray[2].a = 99;<br>copiedArray[3][0] = 44;<br><br>console.log(originalArray); // Output: [1, 2, { a: 3 }, [4, 5]]<br>console.log(copiedArray);   // Output: [1, 2, { a: 99 }, [44, 5]]</pre><p><em>Note:</em> structuredClone() creates a deep copy of a value, allowing for safe copying of complex data structures.</p><p><strong>2- Using </strong><strong>JSON.parse(JSON.stringify()):</strong></p><pre>const originalArray = [1, 2, { a: 3 }, [4, 5]];<br>const copiedArray = JSON.parse(JSON.stringify(originalArray));<br><br>copiedArray[2].a = 99;<br>copiedArray[3][0] = 44;<br><br>console.log(originalArray); // Output: [1, 2, { a: 3 }, [4, 5]]<br>console.log(copiedArray);   // Output: [1, 2, { a: 99 }, [44, 5]]</pre><p><em>Note:</em> This method does not copy functions or undefined values and may not handle special object types like Date or RegExp correctly.</p><p><strong>Conclusion:</strong></p><ul><li>For arrays containing only primitive values, any of the three methods (spread operator, Array.from(), slice()) are safe to use.</li><li>For arrays containing nested objects or arrays, using structuredClone() is the safest method to prevent unintended modifications to the original array.</li><li>If structuredClone() is not available, JSON.parse(JSON.stringify()) is an alternative, though it has limitations.</li><li>Therefore, <strong>structuredClone()</strong> is the safest method among these for preventing unintended modifications to the original array in all cases.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=632297098ab8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Structures — Linked Lists]]></title>
            <link>https://irmakesin.medium.com/data-structures-linked-lists-23d2b54401b2?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/23d2b54401b2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[datastrucutre]]></category>
            <category><![CDATA[arraylist-vs-linkedlist]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Tue, 12 Sep 2023 22:05:46 GMT</pubDate>
            <atom:updated>2023-09-12T22:26:02.154Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data Structures — Linked Lists</h3><p>Linked Lists, consist of nodes connected to each other. When you connect these nodes in one direction, it’s called a <em>Singly Linked List</em>, and when you connect them in two directions, it’s referred to as a <em>Doubly Linked List</em>. We will talk about singly linked lists.</p><p>First, let’s start by <strong>comparing linked lists and arrays</strong>. For instance, let’s draw an array and then transform it into a linked list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*WsqBoE4F5P4mZr-6XgSJKg.png" /><figcaption>Array</figcaption></figure><p>1 — Let’s go step by step. Firstly, <strong><em>linked lists do not have indexes</em></strong> like arrays.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*nS8W3XTAwhYSa3l7vD6YSQ.png" /><figcaption>Indexes have been removed</figcaption></figure><p>2 — Arrays are in contiguous places in memory, but <strong><em>linked lists can be all over the place in memory</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*1_Xb9EvqDuLq3Vicgb5vGA.png" /><figcaption>The connection between them was broken</figcaption></figure><p>3 — Linked Lists are represented <strong><em>graphically in different way</em></strong> and <em>each item</em> in the linked list,<strong> </strong><em>point to the</em><strong><em> next</em></strong>. <em>Last one points to the</em><strong><em> null</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*E203qOzTsuH6bcQtUm4MSw.png" /></figure><p>In more detail:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*-8i-XVmYOd1LL5t-mtFMMg.png" /><figcaption>Linked List (All over the place in memory) / Array (Contiguous memory)</figcaption></figure><p>Let’s take a look at <strong>Big O</strong> for linked lists. If you’re not familiar with Big O, you can read my <a href="https://medium.com/@irmakesin/introduction-to-data-structures-big-o-bc941e1f2399"><em>previous article</em>.</a> For instance, let’s see what happens if we <strong>push a new node to the end of a linked list.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*7RxBmeMxVX4CbUEoi95t8A.png" /><figcaption>Pushing a new node to the end</figcaption></figure><p>We already have direct access to the last node of the list through the tail pointer and simply adjust the next pointer of the current tail node to point to the new node. Then, we update the tail pointer to the new node. The <em>number of steps are same</em> which means this is<strong><em> constant time</em></strong> to <strong><em>push something to the end</em></strong>. <strong>Result is O(1)</strong></p><p>All these steps are basic pointer operations that take constant time each. They don’t require traversing the list or doing any operation that would take more time with a longer list. That’s why the time complexity is <strong>O(1)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*Hhv081jveUWFC3sYOmNWtg.png" /><figcaption>Pushing a new node to the beginning of the linked list gives the same result as pushing it to the end, which is <strong>O(1)</strong></figcaption></figure><p>If we <strong>remove item which we pushed to the end (pop) :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*DVDlTKswlz4br2sU3vqu1w.png" /><figcaption>Removing a node from end</figcaption></figure><p><strong><em>We cannot go backwards in the linked list</em></strong>, because nodes don’t have a <strong>“previous”</strong> pointer. We have to start at the head of the list and traverse the list until you reach that penultimate node. Once we’ve reached the penultimate node, we adjust its “next” pointer to null, effectively removing the tail node from the list. Finally, we set the new tail. So, <strong>Big O is O(1)</strong></p><p><strong>If we remove the head node,</strong> we can simply change the head pointer to point to the next node in the list. So, it doesn’t require traversing any other part of the list, <strong>constant time</strong> again. Our <strong>result is O(1)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*PUfqmyDCYut1Ns-YUCDqeA.png" /><figcaption>Removing a node from head</figcaption></figure><p>If we <strong>insert something into the middle of the linked list :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*8wP2dFPCJYJZb7f5_gxsng.png" /><figcaption>Inserted Linked List</figcaption></figure><p>We have to start the head and iterate through the linked list to get this point. That’s why <strong>Big O is O(n) If we remove the node we inserted</strong>, there will be an iteration again, so the result will still be <strong>O(n)</strong></p><p>So, what do you think is the Big O of <strong>searching for a node in a linked list</strong>? In my opinion, everything is clearer now, and yes, the answer is <strong>O(n)</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*ndMyAcEOIBJPIjktV_4tnQ.png" /><figcaption>Finding an item</figcaption></figure><p><strong><em>We have to start from the beginning again</em></strong>, and since there’s a iteration, our answer is <strong>O(n</strong>). Here, you can see the <strong><em>difference between an array and a linked list</em></strong> more clearly. Finding in an array by value and by index is different. If we search by index in an array, the result is <strong>O(1)</strong>.</p><p>In this article, I introduced linked lists and discussed their logic to make my next article more understandable. In my next article, we’ll write some code related to what I explained today. See you then.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23d2b54401b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Data Structures-Big O]]></title>
            <link>https://irmakesin.medium.com/introduction-to-data-structures-big-o-bc941e1f2399?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc941e1f2399</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[big-o-notation]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Fri, 08 Sep 2023 13:16:52 GMT</pubDate>
            <atom:updated>2023-09-08T15:09:55.561Z</atom:updated>
            <content:encoded><![CDATA[<p>We need different data structures to solve problems. For example, Binary Tree, LinkedList, Array, Stack, etc. Alternatively, on the same data structure, like an Array, we can use different algorithms for sorting, such as Bubble Sort, Insertion Sort, etc. All these choices have consequences for the quality, speed, usability, and resources consumed by our code. Therefore, it’s crucial to know what we’re writing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*e6T1B6jJUn3K4QFk1jxltg.png" /><figcaption>Array</figcaption></figure><p><em>Omega</em> represents the best case, <em>Theta</em> the average case, and <strong><em>Omicron</em></strong> <strong><em>(Big O)</em></strong> the worst case.<strong> </strong>Big O always measuring <strong><em>worst case.</em></strong></p><p>Suppose we have two pieces of code, and both achieve the same thing. How do we decide which one is better? Big O is a way to mathematically understand this, helping us determine which code is <strong><em>more efficient.</em></strong></p><p>Big O understands this in <strong><em>two ways:</em></strong></p><ol><li><strong>Time Complexity </strong>: Scaling with number of operations</li><li><strong>Space Complexity </strong>: Amount of memory that something uses</li></ol><p>Let me show you with an example right away.</p><pre>function logElements(n) {<br>    for(let i = 0; i &lt; n; i++) {<br>        console.log(i)<br>    }<br>}<br><br>logElements(4)</pre><p>We passed the function, the number <strong>n</strong> and this ran <strong>n</strong> times. In example, we passed 10, and it ran 10 times. Because of this, <strong>result is O(n)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*3AJ6OJeje4wejBe7ZcPEKw.png" /><figcaption>O(n) is always going to be a straight line</figcaption></figure><p>There are some ways to simplify Big O, and one of those ways is <strong>Drop Constants</strong>. Let’s try it with an example which includes <strong><em>for loop</em></strong>.</p><pre>function logElements(n) {<br>     for(let i = 0; i &lt; n; i++) {<br>        console.log(i) <br>     }<br> <br>     for(let j = 0; j &lt; n; j++) {<br>        console.log(j) <br>     }       <br> }<br> <br> logElements(4)</pre><p><strong>Output</strong>: 0, 1, 2, 3 for first loop and 0, 1, 2, 3 for second loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*CjzXJpKK9CbV5cgZrfGGcQ.png" /></figure><p>2n, 5n, 400n. The initial constants don’t matter. If there’s a constant, we drop it, and the result becomes <strong>O(n)</strong></p><p>Let’s try to solve example which includes <strong><em>for loop inside of for loop.</em></strong></p><pre>function logElements(n) {<br>     for(let i = 0; i &lt; n; i++) {<br>         for(let j = 0; j &lt; n; j++) {<br>             console.log(i, j) <br>         }       <br>     } <br> }<br> <br> logElements(4)</pre><p>The output starts with 0 0, 1 1, … and stops when it reaches 3 3</p><p>Solution for Big O is n*n, so it’s n2. Result is <strong>O(n2)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*PEKmGtTcj8ZPoV7WpX2OFg.png" /><figcaption>O(n) is better than O(n2)</figcaption></figure><p><strong>Drop Non-Dominants</strong> is another way to simplify Big O</p><pre>function logElements(n) {<br>     for(let i = 0; i &lt; n; i++) {<br>         for(let j = 0; j &lt; n; j++) {<br>             console.log(i, j) <br>         }       <br>     } <br> <br>     for(let k = 0; k &lt; n; k++) {<br>         console.log(k)<br>     }<br> }<br> <br> logElements(4)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*siG8D6jBWCqhKaJYvMsUvw.png" /></figure><p>n2 would be any number here. For instance, it would be 100000, so n cannot significantly affect the result.</p><p>Let’s do some examples with different results.</p><pre>function manyItems(n) {<br>  return n+n+n+n<br>}</pre><p>Whether it’s n+n+n+…+n or just n+n, the result doesn’t change. The Big O is O(1). O(1) represents <em>constant time</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*ggnboJcIiLqP_PB0zxjH1A.png" /><figcaption>O(nlog n) is the efficient as you can make a sorting algorithm</figcaption></figure><p>For now, we’ve just touched on the basics; I’ll go into detail in my subsequent articles. I didn’t mention everything in this article, because some topics are complex. Lastly, I’ve included a Big O efficiency graph here. See you in the upcoming articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc941e1f2399" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Custom React Hooks — useToggle]]></title>
            <link>https://irmakesin.medium.com/creating-custom-react-hooks-usetoggle-c50b8925d503?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/c50b8925d503</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[custom-hook]]></category>
            <category><![CDATA[toggle-switch]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Thu, 28 Jul 2022 11:15:48 GMT</pubDate>
            <atom:updated>2022-07-28T12:16:56.725Z</atom:updated>
            <content:encoded><![CDATA[<h3>Creating Custom React Hooks — useToggle</h3><p>Hi, today I’m going to explain <strong>React Custom Hooks</strong> and show basic example which is <strong>useToggle</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yAwUXJbWEJgf7qvYC_zmLg.jpeg" /></figure><p>Well, <em>why do we use custom hooks, what is the benefit for us?</em> <strong><em>A custom hook is a Javascript function that starts with “use” and that can call other hooks.</em></strong> The main reason to create a custom hook is<strong> code reusability. </strong><em>Instead of writing the same code for different components which use the same logic, we can create a custom hook and reuse it</em><strong><em>.</em></strong> Let’s create toggle hook!</p><p>I’m going to create the <strong><em>toggle hook</em></strong> as I think it is repeated too often. A toggle allows the user to change a setting between two states which are checked and unchecked, each state can trigger its own set of actions.</p><p>We can use toggle hook to <em>tick mark for checkbox, play or pause a video, flip the visibility of anything, etc.</em></p><p>I’m going to show you a toggle switch button that I created to explain better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*6KOtVID93ZuyiXMKV0AwjA.png" /><figcaption>useToggle.js</figcaption></figure><p>First of all, we need one variable and function. Our variable is to point to the Div and a boolean state that determines what we will display or not. The other one is the that makes the toggle function, so, it’s a function that closes if it is open and opens if it is closed.</p><p>useToggle returns the variable which finds out open or close and function which manage that. Then, onToggle returns the reverse open or close.</p><p>The index in the first variable of the returned array, in the second we will send an object. The first variable of the returned array is index which is <strong><em>on</em></strong>, second one is object which is <strong><em>onToggle</em></strong>. Now, we can use useToggle on App.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*aIMZBT6mlZf1JmOScS310Q.png" /><figcaption>App.js</figcaption></figure><p>Finally, we can use custom toggle hook same as we use <a href="https://medium.com/@irmakesin/react-hooks-usestate-37560b54fc5b"><strong><em>useState</em></strong></a>. I specified the initial state as true to stay open at first. By the way, you can give any name you want to the indexes, this is why we return an array in useToggle. Then, I created a button that its onClick triggers onToggle function. After a few scss tricks, it’ll look like this. <em>You can access the all codes from my </em><a href="https://github.com/irmakesin/toggle-switch-react-hooks"><strong><em>github link</em></strong></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*bmPUzLOoNmMNKopPWmMHoQ.png" /><figcaption>toggle switch on</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*tcazXXVpfg8CYGJGovMEag.png" /><figcaption>toggle switch off</figcaption></figure><p>So, <em>custom hooks is a easy way to </em><strong><em>re-use stateful logic</em></strong><em> in react hooks.</em></p><p>It was a little introduction to custom hooks with a pretty short and simple example. If you like my article, I can create and explain 5 custom hooks that the most used in more detail.</p><p>See you in my next article!</p><h3>Sources:</h3><p><a href="https://reactjs.org/docs/hooks-custom.html"><em>React own doc</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c50b8925d503" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks : useRef and forwardRef]]></title>
            <link>https://irmakesin.medium.com/react-hooks-useref-and-forwardref-ae7fe250a641?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae7fe250a641</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Wed, 20 Jul 2022 21:16:02 GMT</pubDate>
            <atom:updated>2022-07-20T21:20:27.881Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks : useRef and forwardRef</h3><p>Hi, it’s me again! Today I’ll show you <strong>useRef </strong>hook and <strong>forwardRef </strong>in react. So, let’s start with useRef hook.</p><p>The <strong>useRef</strong> provides a referance to particular element, and you<br>can access it using that reference. Well what does this mean? <strong>For instance, <em>when we’ve form elements and need to reference these elements to either print their value or focus these elements etc</em>.</strong> Let’s start with an example to make more sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*CYsUcLVt8pjfwByFxkeOGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*molF8N_p9A3b6_1tntBwBg.png" /><figcaption>I created couple of elements here for access them.</figcaption></figure><p>So, this is a form that we enter our informations here, and our aim is when we load the page the focus should be on the username, because that’s the starting of the form. When we fill input and click enter the focus should go to the password, then we enter password and click enter, focus should go to the login button and we should automatically login the page. So, we don’t really have to use our mouse at all. We will use useRef to achieve this, let’s try it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*TSzASlMxhyUQa42WeLCLAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*m4XFASl08UXS5VoMTWXiZw.png" /></figure><p>Firstly, we need to have something where we can actually focus on the username when page loads. Well, I used an <a href="https://medium.com/@irmakesin/react-hooks-useeffect-9fb466aeed9c"><strong><em>useEffect </em></strong></a>for that, because we want to know when the page is finish loading. Then we’ll focus on that username.</p><p>For this, we need to access these elements, so we can focus on them. I created ref for all three elements which are userNameRef, passwordRef and loginRef. When the page is loads, all I can do is using this reference to the username. This is provided by accessing the element with current. So, every useRef reference should have a property called which holds the reference.</p><p>By this means, when the page is refreshed, the focus will be on the username input even if we don’t click input. However, when we click enter, also want it to focus on the input below. To solve this, we need an event and<br>that event is onKeyDown. Let’s see.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*FCyeO97dBDR-pQY655MU_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*50A7vNMUJIXO1UkUr7BZWw.png" /></figure><p>I created 3 functions for keydowns. I need to know if the key is equal to the enter, so I checked that with a synthetic event. I did the same for passworKeyDown. Then, I also added an alert to see a result when the login button is clicked.</p><p>Finally, I added onKeyDown event to my 2 inputs and buttons, and called the functions which I created in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*NfhIrfRby1FafDuqiRGc7Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*o1hQ_HjRt5a96_bkEW2Ydg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*Mya61HKW4M91GDLdLhwxgA.png" /></figure><p>Thus, while filling the inputs, we moved to the other input and focused on <strong>without using a mouse</strong>.</p><p>I hope I was able to explain what useRef does. Well, there is one more point we should mention. The input is a jsx element in the example that I gave, but what about a component? How we deal with that? At this point <strong>forwardRef </strong>comes into picture. <strong><em>Purpose of forwardRef is forwarding the reference into component</em>.</strong> Let’s write an example to understand better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*CdrVFf8VvRbJOKHO4FaOhA.png" /><figcaption>Input Component</figcaption></figure><p>Firstly, I created a basic Input component, then passed props we need here. Our <em>first argumen</em>t should be props which are <strong>type</strong>, <strong>onKeyDown </strong>and <strong>placeholder</strong>,<strong> </strong>then we get <strong>ref </strong>as a <em>second argument</em> here.</p><p>Then, I used forwardRef and wrapped my input in this. There is only one easy part left!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*gvgsXJRxXBJBgX1W20eOKg.png" /></figure><p>Here, all we need to do is to import the Input component and convert the inputs with jsx elements into components. Now, it will work the same way as in useRef!</p><p>To sum up, <strong>useRef refs jsx elements, forwardRef refs components.</strong></p><p>We’ve come to an end of this article, see you in next!</p><h3>Sources</h3><p><a href="https://reactjs.org/docs/hooks-reference.html#useref"><em>React Doc</em></a></p><p><a href="https://www.youtube.com/watch?v=KRQ97fhnR3A"><em>Prototurk Youtube Channel</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae7fe250a641" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks : useEffect]]></title>
            <link>https://irmakesin.medium.com/react-hooks-useeffect-9fb466aeed9c?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fb466aeed9c</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[useeffect]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Tue, 19 Jul 2022 14:14:24 GMT</pubDate>
            <atom:updated>2022-07-19T14:15:16.323Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks : useEffect</h3><p>Hi everyone! I will continue the series with another frequently used react hook <strong>useEffect</strong>. If you haven’t seen my article about the useState hook, you can read it <a href="https://medium.com/@irmakesin/react-hooks-usestate-37560b54fc5b"><strong>here</strong></a>. I would recommend you to read, as there will be state hook in the examples I will give you today.</p><p>If you remember <em>class-based</em> components; probably you’re familiar with <strong>componentDidMount</strong>, <strong>componentDidUpdate </strong>and <strong>componentWillUnmount </strong>lifecyle methods. We can say that <em>useEffect</em> hook, can provide the same functionality as the three lifecycle methods mentioned above.</p><p>To summarize useEffect briefly, while running the application, a method we use to call for side effects on which the application is dependent. These side effects can change according to our needs. <em>Getting data, manually modifying the DOM, various events waiting for getting from the window object etc</em>.</p><p>So, how to use useEffect? We’ll create a test component at first. Let’s dive in!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*S4HkCSueh7TGjWnsWZvCvg.png" /><figcaption>The first effect hook is equivalent to componentDidUpdate in class components and the second one is componentDidMount</figcaption></figure><p><strong>useEffect takes 2 parameters</strong>. The first parameter is a <strong>callback function,<br>the codes that run are included in this function</strong>. If we give an <strong>empty array</strong> as I used in the first useEffect, the component will run when it is loaded first. If I leave the 2nd parameter <strong>empty </strong>like second useEffect, the component will work on every render. Let’s see!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*S5HjovGsWOOulnbS-yJgOw.png" /></figure><p>Let’s change the example a little more by using the useState I explained in my last article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*loBpa1INXkI51OUi8NGruQ.png" /><figcaption>I create a button named show if the show state is true, hide if not</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/103/1*6HYFt9lfcMfJ8v6NxdyngQ.png" /><figcaption>show button</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*rXOFFYPiMrM4p4Xlz9QZbA.png" /><figcaption>hide button</figcaption></figure><p>As you can see, this time the test component only appears when we click the show button. So what should we do if we want it to be destroyed when we click the hide button? Let’s look at this now!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*vqoLQ7ycjAH3sAeGAnLcOA.png" /></figure><p>I returned a function at the end of my first useEffect, <em>it will work when the component is unmounted</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*iOoKvKpNuXH5mOqESYFuxg.png" /></figure><p>Now that we’ve caught the<strong> </strong>destroy side effect,<em> we’ve seen 3 lifescyles with the test component</em>. Now let’s create a counter inside test component to see that test component is rendered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*qhfTjSfZnJ3Db0k7s-Olgw.png" /></figure><p>Each time we click the Arttır(Increase) button, the value of count will increase by one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*9dHU2LpXzb7S_ng5XybOWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*1Ky-4NAiauccQI9ADC0ePA.png" /></figure><p>As you can see, each time we click the increase(arttır) button, the component is rendered. <strong>Let’s see what happens if we give a dependency to the second parameter.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*_slFI-rmtvOjyWvEy8XijQ.png" /></figure><p>Our aim here, listening to count state. So every time count changes, we want to know it and get the new count value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*qeOMLhwRolIWeexZzu5Tuw.png" /><figcaption>First value assigned to count</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*Ga9kIYRx5R2J1RKRNrZKiw.png" /><figcaption>Every count value that changes</figcaption></figure><p>Thus, every time we click on the arttır button, we understand that the component is rendered. Let’s change the example a bit for better realizing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*-T5f4THnDM7sILHQhXWktw.png" /></figure><p>I create two states that one of them is postId, and other one is post instead of count state. If we’ve postId, it’ll send fetch request. Also, if we’ve post, expect to see the details of the post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*B2VNdGgBgrXJbAGudtLrAg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*hu9gooy8-ATvlcTbbOZ6bA.png" /></figure><p>When we click the show button, it sent request and gave us the post that id 1. As we click the continue button, it’ll show us the posts in order. <strong>Well, why we sent a request inside the useEffect?</strong> <strong><em>Due to useEffect, we get the data of that id and update our state every time the id changes, without making a request again and again.</em></strong> Generally we need to send a request when the page first loads, now let’s see why!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*eOx6bFPuV12cEChxWZJqWw.png" /><figcaption>I sent a request in a random part of the page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*nePEaL6s_Ul2D9whaVGfzQ.png" /><figcaption>Works 4 times</figcaption></figure><p>This means that the component is rendered 4 times. So this place works every time it renders. However, it not preferred as it is a cost and can causes to errors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*a-VCtTBdoTwkmxTXqNw0Wg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*vnrwNiuB2Igl316kSG_Xdw.png" /></figure><p>If we get it here, works once when the component’s first load. It doesn’t affect it unless we give it a dependency afterwards. We’ll give one last example and finish the useEffect hook!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*l4xMF2XWr3yoheGRpl4HfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*5TxNrmYxZrU4Nl4MDlsSWA.png" /></figure><p>Here I create an interval that starts running when I click the show button. However, even though I click the hide button, we can see the something is still working in the background. Let’s fix this issue!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*8xQIyewHIIzTzuSPAo3fWw.png" /></figure><p>If we assign setInterval to a variable and then clear it with clearInterval our issue will be solved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*GyIHGA-KtkShHXaTiJSGIg.png" /></figure><p>As you can see, the interval is cleared when I click the hide button. So, we’ve seen in this example that the component is destroyed.</p><p>Take care, see you in my next hook article!</p><h3>Sources</h3><p><a href="https://www.youtube.com/watch?v=slVS7QNsSk8&amp;list=PLfAfrKyDRWrGXWpnJdyC4yXIW6v-PcFu-"><em>Prototurk youtube channel</em></a></p><p><a href="https://reactjs.org/docs/getting-started.html"><em>React Doc</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fb466aeed9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks : useState]]></title>
            <link>https://irmakesin.medium.com/react-hooks-usestate-37560b54fc5b?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/37560b54fc5b</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Mon, 18 Jul 2022 15:31:17 GMT</pubDate>
            <atom:updated>2022-07-20T10:25:04.364Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks : useState</h3><p>As you know, hooks are new addition in React 16.8. and let you use some React features without writing a class. If you’re not familiar with hooks, I suggest you look into react’s own <a href="https://reactjs.org/docs/hooks-intro.html"><strong>documentation </strong></a>before reading this article.</p><p>In this article, I will talk about <strong>useState</strong>, one of the most used hooks.</p><p>useState is a React Hook that <strong>keeps our data between renders</strong>. Our Component function is cleaned and computed again on every render. Therefore, <strong>when our variables and data are computed again, they return to their initial values</strong>. To solve this problem we keep our data with useState.</p><p>For better understanding of <strong>useState</strong>, I want to show a little <strong>counter and message app.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*XMe7woxd61oHbFod_TcqMQ.png" /></figure><p><strong>useState expects an initial value</strong> from us. In contrast with, an array is returned. I get the 0th element of the returned array as count and the 1st element as setCount. While learning the current state value with count, I will <strong>update my state with the setCount function.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*lPe6PSHT-XGmb4xL1YcIog.png" /></figure><p>Here we printed the count value on the screen, and we added the buttons below that allow us to increase or decrease the count value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*7yj1NpQ7uiSuK45g62SMew.png" /></figure><p>We did the same for the message as we did for the counter. Now every time we click the <strong>increase button</strong>, the counter will increase, and when we click the <strong>decrease button</strong>, the value will decrease. Also, when we click the <strong>change button</strong>, the message will change. Let’s see.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*eY3pIXfpSF_g1geexuXG3w.png" /></figure><p>As you can see, the state value we hold has changed when we click the buttons. In other words, <strong>we assigned a new value to our current state with set functions.</strong></p><p>I hope it was a useful article, don’t forget to stay tuned for the continuation of the hooks series.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37560b54fc5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SOLID Design Principles Explained: The Single Responsibility Principle]]></title>
            <link>https://irmakesin.medium.com/solid-design-principles-explained-the-single-responsibility-principle-1cb999aa0aa6?source=rss-a175972bdf------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cb999aa0aa6</guid>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[solid]]></category>
            <category><![CDATA[single-responsibility]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Irmak Esin]]></dc:creator>
            <pubDate>Sun, 06 Jun 2021 08:06:00 GMT</pubDate>
            <atom:updated>2021-06-06T08:46:46.657Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*qL8zEyTgCF4UMQEXokltyw.jpeg" /></figure><p>Hi everyone, in this article I will talk about the <strong>single responsibility</strong> from <strong>SOLID </strong>principles. Then let’s see what single responsibility principle is. As its very name signifies, it is a principle designed that each class has a single responsibility for its method. Simply, our <strong>classes </strong>should have a <strong>single </strong>and <strong>well-defined</strong> responsibility. A class (object) can be changed for only one purpose, that purpose is the responsibility assigned to that class. So, a class has <strong>only one responsibility </strong>to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z2ErzT9Q5ePrAMGso1WkcQ.png" /></figure><p>With this picture, everything will become clearer and visualize in our minds. You can see the class in the first picture has multiple responsibilities, so do you think this is an effective way? Imagine you want to replace one of these responsibilities. For example, we no longer want a driver but an engineer. Guys, this is where the single principle responsibility becomes important.</p><p>When our requirements change, we will need to make changes to the entire class, since we assign multiple responsibilities to one class. Why should other three responsibilities be affected although they are irrelevant with the change? If we split the responsibilities in a purposeful way, we should have no more such problems.</p><p>Like in this software world; a class changes as much as the responsibility it takes. <strong>Thus, while causing many pieces of code to change, in rewriting; changes become difficult to implement.</strong> Thus, if we summarize the benefits of single principle responsibility in 3 articles:</p><p><strong>1- The class is easier to understand</strong></p><p><strong>2- The class is easier to maintain</strong></p><p><strong>3- The class is more reusable</strong></p><p>Let’s take a look at a simple code snippet to understand better. Starting with a bad example will be more clear :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*2E4V733KBhMPpz6Va4JLEQ.png" /></figure><p><strong>The DeployingService class above handles the responsibilites of both deploying and checking if the user is authorized.</strong> Do you think it is effective that the control of the authorized person is in the <strong>deployingService class</strong>? Let’s create a new class for this control.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*0DezoNiFY7xBgS_KdWr1WQ.png" /><figcaption><strong>DeployingService Class</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*U2bX8lt1DpXy0KWFVccFCw.png" /><figcaption><strong>EmployeesService Class</strong></figcaption></figure><p>So, we implemented single responsibility principle simply! <strong>In this case, both classes have their own responsibilities.</strong> Then, it will be easy to update the code and the case of chance for any place in the code to break the other will be reduced.</p><p>I hope this article was useful for you :) Don’t forget, <strong>Clean code = Happy team</strong></p><p>Avoid from spaghetti coding!</p><p><strong>Resources:</strong></p><ul><li><a href="https://springframework.guru/principles-of-object-oriented-design/single-responsibility-principle/">Single Responsibility Principle</a></li><li><a href="https://yazilimcigenclik.com.tr/solid-yazilim-gelistirme-prensipleri/">SOLID Yazılım Geliştirme Prensipleri</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cb999aa0aa6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>