<?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: Sabber Hossain</title>
    <description>The latest articles on DEV Community by Sabber Hossain (@engrsabber11).</description>
    <link>https://dev.to/engrsabber11</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%2F2726237%2F81d6464c-46a5-4a09-8891-2af3a277fc7a.jpg</url>
      <title>DEV Community: Sabber Hossain</title>
      <link>https://dev.to/engrsabber11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/engrsabber11"/>
    <language>en</language>
    <item>
      <title>Level Up Your React Skills: 10 Advanced Techniques for Senior Devs</title>
      <dc:creator>Sabber Hossain</dc:creator>
      <pubDate>Thu, 30 Jan 2025 03:08:17 +0000</pubDate>
      <link>https://dev.to/engrsabber11/level-up-your-react-skills-10-advanced-techniques-for-senior-devs-1103</link>
      <guid>https://dev.to/engrsabber11/level-up-your-react-skills-10-advanced-techniques-for-senior-devs-1103</guid>
      <description>&lt;p&gt;As React applications grow more complex, the patterns that were “just fine” when you were starting out might start to feel limiting. Maybe you’ve built a successful MVP, but now you’re noticing subtle performance issues. Or perhaps your state management has gotten tangled, and your data fetching logic has mushroomed into something unrecognisable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Use useCallback with a Persistent Service Reference&lt;/strong&gt;&lt;br&gt;
We often see useCallback used to memoize inline arrow functions in event handlers. We do this to make sure that the function reference remains stable and does not trigger unnecessary re-renders when passed as a prop.&lt;/p&gt;

&lt;p&gt;But as you level up, you’ll find you can use it to maintain stable references to more complex services—like WebSockets, workers, or other persistent resources—so they aren’t re-created unnecessarily on each render.&lt;/p&gt;

&lt;p&gt;This approach builds on the basics of useRef and useCallback: you’re ensuring a long-lived service connection remains stable. This can save performance overhead and avoid unintended reconnections.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createExpensiveService() {
  // Pretend this sets up a WebSocket or a shared worker
  return { send: (msg) =&amp;gt; console.log('Sending:', msg) };
}

function usePersistentService() {
  const serviceRef = React.useRef(createExpensiveService());
  // Memoize the send function so it never changes
  const stableSend = React.useCallback((msg) =&amp;gt; {
    serviceRef.current.send(msg);
  }, []);
  return stableSend;
}
function MyComponent() {
  const send = usePersistentService();
  return &amp;lt;button onClick={() =&amp;gt; send('HELLO')}&amp;gt;Send Message&amp;lt;/button&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Using a Ref for Simplicity Instead of State&lt;/strong&gt;&lt;br&gt;
Sometimes we make the mistake to put all changing data in a state. But sometimes, you just need a value that won’t trigger a re-render. In these cases, using a ref is actually simpler and more efficient.&lt;/p&gt;

&lt;p&gt;For example, imagine a counter that you just need to read and update internally without affecting the UI. A ref is perfect. No need for useState or fancy renders—just a stable box to store a changing value.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyCounter() {
  const counterRef = React.useRef(0);
  const increment = () =&amp;gt; {
    counterRef.current++;
    console.log('Ref count is now:', counterRef.current);
  };
  return &amp;lt;button onClick={increment}&amp;gt;Increment (Check Console)&amp;lt;/button&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Using Suspense for Data Fetching with a Global Resource Cache&lt;/strong&gt;&lt;br&gt;
In many React apps, fetching data involves useEffect, loading states, and a bunch of manual checks. Suspense can simplify all of this by allowing components to “read” from a special data resource. If the data isn’t ready, the component automatically suspends, and React shows a fallback UI until the data arrives. This approach centralises loading logic, making your components cleaner and more focused on rendering.&lt;/p&gt;

&lt;p&gt;Example (Conceptual):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createResource(fetchFn) {
  let status = 'pending';
  let result;
  const promise = fetchFn().then(
    data =&amp;gt; { status = 'success'; result = data; },
    err =&amp;gt; { status = 'error'; result = err; }
  );
  return {
    read() {
      if (status === 'pending') throw promise;
      if (status === 'error') throw result;
      return result;
    }
  };
}

const userResource = createResource(() =&amp;gt; fetch('/api/user').then(r =&amp;gt; r.json()));

function UserProfile() {
  const user = userResource.read();
  return &amp;lt;div&amp;gt;Hello, {user.name}!&amp;lt;/div&amp;gt;;
}

function App() {
  return (
    &amp;lt;React.Suspense fallback={&amp;lt;div&amp;gt;Loading user data...&amp;lt;/div&amp;gt;}&amp;gt;
      &amp;lt;UserProfile /&amp;gt;
    &amp;lt;/React.Suspense&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Suspense with Dynamic Imports and Preload Hints&lt;/strong&gt;&lt;br&gt;
Code splitting is common with React.lazy(), but you can take it further by preloading code before the user needs it. This reduces the wait time when they finally click a button or navigate to a certain route. Start loading your heavy components in the background so that they are instantly ready when needed.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const HeavyChart = React.lazy(() =&amp;gt; import('./HeavyChart'));

function usePreloadHeavyChart() {
  React.useEffect(() =&amp;gt; {
    import('./HeavyChart'); // start preloading on mount
  }, []);
}

function Dashboard() {
  usePreloadHeavyChart();
  return (
    &amp;lt;React.Suspense fallback={&amp;lt;div&amp;gt;Loading Chart...&amp;lt;/div&amp;gt;}&amp;gt;
      &amp;lt;HeavyChart /&amp;gt;
    &amp;lt;/React.Suspense&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Error Boundaries that Retry Automatically&lt;/strong&gt;&lt;br&gt;
At some point, something in your app may fail — maybe a network request or a lazy-loaded component. Traditional error boundaries show a fallback UI and stop there. By enhancing them, you can try recovering automatically, retrying after a short delay. This can be a great user experience improvement, turning a temporary glitch into a seamless recovery.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AutoRetryErrorBoundary extends React.Component {
  state = { hasError: false, attempt: 0 };
  static getDerivedStateFromError() {
    return { hasError: true };
  }
  componentDidUpdate() {
    if (this.state.hasError) {
      setTimeout(() =&amp;gt; {
        this.setState(s =&amp;gt; ({ hasError: false, attempt: s.attempt + 1 }));
      }, 2000);
    }
  }
  render() {
    if (this.state.hasError) return &amp;lt;div&amp;gt;Retrying...&amp;lt;/div&amp;gt;;
    return this.props.children(this.state.attempt);
  }
}

function UnstableComponent({ attempt }) {
  if (attempt &amp;lt; 2) throw new Error('Simulated Crash!');
  return &amp;lt;div&amp;gt;Loaded on attempt {attempt}&amp;lt;/div&amp;gt;;
}

// Usage
&amp;lt;AutoRetryErrorBoundary&amp;gt;
  {(attempt) =&amp;gt; &amp;lt;UnstableComponent attempt={attempt} /&amp;gt;}
&amp;lt;/AutoRetryErrorBoundary&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Virtualising Lists with Dynamic Item Height&lt;/strong&gt;s&lt;br&gt;
When you have huge lists, rendering every item can kill performance. Virtualisation libraries (like react-window) render only what’s visible. But what if items have unpredictable heights? You can measure them dynamically and feed those measurements back into your virtualisation logic. This reduces both memory usage and rendering time, keeping scrolling silky smooth.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { VariableSizeList as List } from 'react-window';

function useDynamicMeasurement(items) {
  const sizeMap = React.useRef({});
  const refCallback = index =&amp;gt; el =&amp;gt; {
    if (el) {
      const height = el.getBoundingClientRect().height;
      sizeMap.current[index] = height;
    }
  };
  const getSize = index =&amp;gt; sizeMap.current[index] || 50;
  return { refCallback, getSize };
}

function DynamicHeightList({ items }) {
  const { refCallback, getSize } = useDynamicMeasurement(items);
  return (
    &amp;lt;List height={400} itemCount={items.length} itemSize={getSize} width={300}&amp;gt;
      {({ index, style }) =&amp;gt; (
        &amp;lt;div style={style} ref={refCallback(index)}&amp;gt;
          {items[index]}
        &amp;lt;/div&amp;gt;
      )}
    &amp;lt;/List&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. Using a State Machine for Complex UI Flows&lt;/strong&gt;&lt;br&gt;
When your component starts to feel like a spaghetti mess of if statements, a state machine can help. Tools like XState integrate nicely with React hooks. Instead of juggling multiple booleans, you define states and transitions in a single, clean chart. It’s a mental model shift: you’re writing a “map” of how your UI flows, making it easier to understand and debug.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useMachine } from '@xstate/react';
import { createMachine } from 'xstate';

const formMachine = createMachine({
  initial: 'editing',
  states: {
    editing: { on: { SUBMIT: 'validating' } },
    validating: {
      invoke: {
        src: 'validateForm',
        onDone: 'success',
        onError: 'error'
      }
    },
    success: {},
    error: {}
  }
});

function Form() {
  const [state, send] = useMachine(formMachine, {
    services: { validateForm: async () =&amp;gt; {/* validation logic */} }
  });
  return (
    &amp;lt;button onClick={() =&amp;gt; send('SUBMIT')}&amp;gt;
      {state.value === 'editing' ? 'Submit' : state.value.toString()}
    &amp;lt;/button&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8. Controlled Concurrency with useTransition and Task Queues&lt;/strong&gt;&lt;br&gt;
React 18 introduced useTransition to help you mark certain state updates as “non-urgent.” This can be a game-changer for performance under heavy load. Imagine fetching large amounts of data or performing expensive calculations. By deferring non-urgent updates, you keep the UI responsive and avoid locking up the main thread.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ComplexUI() {
  const [isPending, startTransition] = React.useTransition();
  const [data, setData] = React.useState([]);

  function loadMore() {
    startTransition(() =&amp;gt; {
      setData(old =&amp;gt; [...old, ...generateMoreData()]);
    });
  }
  return (
    &amp;lt;&amp;gt;
      &amp;lt;button onClick={loadMore}&amp;gt;Load More&amp;lt;/button&amp;gt;
      {isPending &amp;amp;&amp;amp; &amp;lt;span&amp;gt;Loading more data...&amp;lt;/span&amp;gt;}
      &amp;lt;List data={data} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;9. useImperativeHandle to Create Controlled Component APIs&lt;/strong&gt;&lt;br&gt;
Sometimes you need a parent component to directly control a child — like calling childRef.current.focus() on a custom input. useImperativeHandle is a hook that lets you define what a parent sees when it uses ref on a child component. This is perfect for creating neat, controlled component APIs that feel like calling a method on a class instance, but in a React-friendly way.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const FancyInput = React.forwardRef((props, ref) =&amp;gt; {
  const inputRef = React.useRef();
  React.useImperativeHandle(ref, () =&amp;gt; ({
    focus: () =&amp;gt; inputRef.current.focus(),
    getValue: () =&amp;gt; inputRef.current.value
  }));
  return &amp;lt;input ref={inputRef} {...props} /&amp;gt;;
});

function Parent() {
  const fancyRef = React.useRef();
  return (
    &amp;lt;&amp;gt;
      &amp;lt;FancyInput ref={fancyRef} /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; fancyRef.current.focus()}&amp;gt;Focus Input&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;10. Progressive Hydration with a Custom Hook&lt;/strong&gt;&lt;br&gt;
Server-side rendering (SSR) can get your content to the user fast, but hydrating a huge app all at once can slow interactivity. By delaying hydration for non-critical parts of your page, you can keep the initial load snappy. A custom hook that gradually hydrates certain components after a delay can make SSR feel even more seamless.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useProgressiveHydration(delay = 1000) {
  const [hydrated, setHydrated] = React.useState(false);
  React.useEffect(() =&amp;gt; {
    const t = setTimeout(() =&amp;gt; setHydrated(true), delay);
    return () =&amp;gt; clearTimeout(t);
  }, [delay]);
  return hydrated;
}

function HeavyComponent() {
  const hydrated = useProgressiveHydration();
  return hydrated ? &amp;lt;ExpensiveTree /&amp;gt; : &amp;lt;Placeholder /&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Final Thoughts&lt;br&gt;
These techniques aren’t meant to be used all at once, and certainly not in every codebase. They’re tools — advanced ones — that become helpful as your applications and your skills grow. Consider these approaches when you start running into the limitations of simpler patterns.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>MySQL Partitioning Guide: Improve Query Performance with Range, List, and Hash Partitioning</title>
      <dc:creator>Sabber Hossain</dc:creator>
      <pubDate>Fri, 24 Jan 2025 03:16:59 +0000</pubDate>
      <link>https://dev.to/engrsabber11/mysql-partitioning-guide-improve-query-performance-with-range-list-and-hash-partitioning-5p9</link>
      <guid>https://dev.to/engrsabber11/mysql-partitioning-guide-improve-query-performance-with-range-list-and-hash-partitioning-5p9</guid>
      <description>&lt;p&gt;MySQL পার্টিশনিং একটি ডেটাবেস অপ্টিমাইজেশন টেকনিক যা বড় টেবিলকে ছোট, সহজে ব্যবস্থাপনা করা যায় এমন অংশে বিভক্ত করে। এটি ডেটার সাথে কাজ করা সহজ করে তোলে, কোয়েরি পারফরম্যান্স বৃদ্ধি করে এবং বড় ডেটাবেজের স্কেলেবিলিটি নিশ্চিত করে।&lt;/p&gt;

&lt;p&gt;MySQL-এ পার্টিশনিংয়ের ধারণা&lt;br&gt;
পার্টিশনিং এমন একটি প্রক্রিয়া যা একটি টেবিলের ডেটা বিভিন্ন অংশে (partition) ভাগ করে, যেখানে প্রতিটি অংশ টেবিলের একটি লজিক্যাল সাবসেট। যখন একটি কোয়েরি চালানো হয়, তখন ডেটাবেজ ইঞ্জিন শুধুমাত্র প্রাসঙ্গিক পার্টিশনে কাজ করে, যা ডেটা স্ক্যানের পরিমাণ কমিয়ে আনে এবং কার্যক্ষমতা বৃদ্ধি করে।&lt;/p&gt;

&lt;p&gt;পার্টিশনিংয়ের ধরন&lt;br&gt;
&lt;strong&gt;১. Horizontal Partitioning&lt;/strong&gt;&lt;br&gt;
এটি ডেটার সারিগুলিকে বিভক্ত করার প্রক্রিয়া। প্রতিটি পার্টিশনে সমান সংখ্যক কলাম থাকে, কিন্তু সারির সংখ্যা আলাদা হতে পারে। MySQL-এর পার্টিশনিং মূলত এই প্রকারের উপর নির্ভর করে।&lt;/p&gt;

&lt;p&gt;MySQL-এ আনুভূমিক পার্টিশনিং প্রধানত তিনটি ভিন্ন পদ্ধতিতে করা যায়:&lt;/p&gt;

&lt;p&gt;ক. রেঞ্জ পার্টিশনিং (Range Partitioning)&lt;br&gt;
রেঞ্জ পার্টিশনিংয়ের ক্ষেত্রে, একটি নির্দিষ্ট কলামের মান যদি একটি নির্দিষ্ট পরিসরের (range) মধ্যে পড়ে, তবে সেই সারি সংশ্লিষ্ট পার্টিশনে রাখা হয়।&lt;br&gt;
উদাহরণস্বরূপ, যদি আপনার একটি sales টেবিল থাকে এবং এটি year কলামের ভিত্তিতে পার্টিশন করা হয়:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE sales (
    id INT,
    amount DECIMAL(10, 2),
    year INT
)
PARTITION BY RANGE (year) (
    PARTITION p1 VALUES LESS THAN (2000),
    PARTITION p2 VALUES LESS THAN (2010),
    PARTITION p3 VALUES LESS THAN (2020),
    PARTITION p4 VALUES LESS THAN MAXVALUE
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;এখানে, ২০০০-এর আগে, ২০১০-এর আগে, এবং ২০২০-এর আগে আলাদা আলাদা পার্টিশনে ডেটা বিভক্ত হবে।&lt;/p&gt;

&lt;p&gt;খ. লিস্ট পার্টিশনিং (List Partitioning)&lt;br&gt;
লিস্ট পার্টিশনিংয়ে একটি কলামের নির্দিষ্ট মানের তালিকা অনুযায়ী সারিগুলো বিভিন্ন পার্টিশনে বিভক্ত করা হয়।&lt;br&gt;
উদাহরণস্বরূপ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE sales (
    id INT,
    region VARCHAR(50),
    amount DECIMAL(10, 2)
)
PARTITION BY LIST COLUMNS (region) (
    PARTITION p1 VALUES IN ('North', 'East'),
    PARTITION p2 VALUES IN ('South', 'West')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;এখানে অঞ্চল অনুযায়ী ডেটা বিভক্ত হচ্ছে।&lt;/p&gt;

&lt;p&gt;গ. হ্যাশ পার্টিশনিং (Hash Partitioning)&lt;br&gt;
হ্যাশ পার্টিশনিংয়ের ক্ষেত্রে একটি কলামের মান একটি হ্যাশ ফাংশনের মাধ্যমে বিভক্ত করা হয়। এটি ডেটা সমানভাবে বিতরণ নিশ্চিত করে।&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE sales (
    id INT,
    amount DECIMAL(10, 2)
)
PARTITION BY HASH (id) PARTITIONS 4;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;এখানে, id কলামের মানের উপর ভিত্তি করে চারটি পার্টিশনে ডেটা স্বয়ংক্রিয়ভাবে বিভক্ত হবে।&lt;/p&gt;

&lt;p&gt;২. উল্লম্ব পার্টিশনিং (Vertical Partitioning)&lt;br&gt;
উল্লম্ব পার্টিশনিং হল ডেটার কলামগুলিকে বিভক্ত করার প্রক্রিয়া। প্রতিটি টেবিলে কিছু কলাম রাখা হয়, এবং অবশিষ্ট কলামগুলি ভিন্ন টেবিলে সংরক্ষণ করা হয়। যেহেতু MySQL উল্লম্ব পার্টিশনিং সমর্থন করে না, এটি ম্যানুয়ালি তৈরি করতে হয়।&lt;/p&gt;

&lt;p&gt;উদাহরণস্বরূপ:&lt;br&gt;
একটি বড় টেবিল users-এর সমস্ত কলাম একটি টেবিলে না রেখে, আপনি এটি দুটি টেবিলে বিভক্ত করতে পারেন:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_personal (নাম, ইমেল, ফোন নম্বর)
user_credentials (ইউজারনেম, পাসওয়ার্ড)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MySQL পার্টিশনিং বড় ডেটাসেট পরিচালনা এবং পারফরম্যান্স অপ্টিমাইজেশনের জন্য অত্যন্ত কার্যকর। তবে এটি ব্যবহার করার আগে ডেটার প্রকৃতি এবং কোয়েরি প্যাটার্ন বোঝা অত্যন্ত গুরুত্বপূর্ণ। সঠিকভাবে পরিকল্পনা এবং বাস্তবায়ন করা হলে, পার্টিশনিং আপনার ডেটাবেজ সিস্টেমকে আরও দ্রুত, কার্যকর এবং সহজে পরিচালনাযোগ্য করে তুলতে পারে।&lt;/p&gt;

&lt;p&gt;আরো বিস্তারিত জানতেঃ&lt;br&gt;
&lt;a href="https://tinyurl.com/bddj776w" rel="noopener noreferrer"&gt;https://tinyurl.com/bddj776w&lt;/a&gt;&lt;br&gt;
&lt;a href="https://tinyurl.com/eueh24j6" rel="noopener noreferrer"&gt;https://tinyurl.com/eueh24j6&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Component Mounting with Rails: Simplifying Dynamic Interfaces</title>
      <dc:creator>Sabber Hossain</dc:creator>
      <pubDate>Wed, 22 Jan 2025 02:12:55 +0000</pubDate>
      <link>https://dev.to/engrsabber11/react-component-mounting-with-rails-simplifying-dynamic-interfaces-33e0</link>
      <guid>https://dev.to/engrsabber11/react-component-mounting-with-rails-simplifying-dynamic-interfaces-33e0</guid>
      <description>&lt;p&gt;Modern apps often require dynamic interfaces that can handle API calls, dynamic forms, and routing, and React is an excellent choice for this. However, React’s SPA (Single Page Application) approach doesn’t align well with Ruby on Rails, a full-stack framework that renders content server-side. While you can use React with Rails by setting up an API backend or using Webpack integration, this setup can add significant complexity and development time.&lt;/p&gt;

&lt;p&gt;The Challenge&lt;br&gt;
In projects like Raive and Dunu506, we needed dynamic features like complex wizards that required real-time interaction. Rails' traditional server-side rendering was cumbersome for these use cases, and React's dynamic nature was a clear fit.&lt;/p&gt;

&lt;p&gt;But how could we use React without going full SPA? The solution is simple: we can mount React components in Rails views without abandoning Rails' server-side rendering approach.&lt;/p&gt;

&lt;p&gt;The Solution&lt;br&gt;
React components can be mounted on specific elements in a server-rendered view. Here's how we started:&lt;/p&gt;

&lt;p&gt;Basic Mounting: In the app/javascript/packs/application.js, we mounted a simple React component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom'
import Hello from './Hello'

document.addEventListener('DOMContentLoaded', () =&amp;gt; {
  const div = document.querySelector('#hello')

  if (div) {
    const props = JSON.parse(div.getAttribute('data-props'))
    ReactDOM.render(
      &amp;lt;Hello {...props} /&amp;gt;,
      div
    )
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reusability: We then created a reusable function to mount components dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import MyReactComponent from './MyReactComponent'

function mountComponent(id, Component) {
  document.addEventListener('DOMContentLoaded', () =&amp;gt; {
    const div = document.querySelector(id)

    if (div) {
      const props = JSON.parse(div.getAttribute('data-props'))
      ReactDOM.render(
        &amp;lt;Component {...props} /&amp;gt;,
        div
      )
    }
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;mountComponent('#my-react-component', MyReactComponent)&lt;br&gt;
Handling Multiple Components: For cases where we wanted to reuse the same component in multiple places, we generalized the approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function mountComponent(selector, Component) {
  document.addEventListener('DOMContentLoaded', () =&amp;gt; {
    const elements = document.querySelectorAll(selector)

    elements.forEach(element =&amp;gt; {
      const props = JSON.parse(element.getAttribute('data-props'))
      ReactDOM.render(
        &amp;lt;Component {...props} /&amp;gt;,
        element
      )
    })
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Avoiding Conflicts: Instead of using id or class, we decided to use a custom data-component attribute to avoid conflicts with other CSS or JS classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function mountComponents(components) {
  document.addEventListener('DOMContentLoaded', () =&amp;gt; {
    const roots = document.querySelectorAll('[data-component]')

    Array.from(roots).forEach((root) =&amp;gt; {
      const props = JSON.parse(root.dataset.props)
      const Component = components[root.dataset.component]

      ReactDOM.render(
        &amp;lt;Component {...props} /&amp;gt;,
        root
      )
    })
  })
}

mountComponents({
  MyReactComponent
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Result&lt;br&gt;
This solution provided a clean, reusable way to mount React components in Rails views. It allowed us to leverage the dynamic power of React while maintaining the simplicity of Rails server-side rendering. We packaged this approach into a library that can be used in any project, saving development time and streamlining our workflow.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
By using a data-component attribute and creating a simple function to mount React components dynamically, we successfully combined the power of React with the traditional server-rendered approach of Rails. This method is flexible, reusable, and clean, making it a great choice for teams needing dynamic interfaces in a Rails app.&lt;/p&gt;

&lt;p&gt;Feel free to check out our GitHub repository for more details and to collaborate on this project!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>productivity</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Laravel Tip: whereHas or whereRelation?</title>
      <dc:creator>Sabber Hossain</dc:creator>
      <pubDate>Fri, 17 Jan 2025 02:49:42 +0000</pubDate>
      <link>https://dev.to/engrsabber11/laravel-tip-wherehas-or-whererelation-240d</link>
      <guid>https://dev.to/engrsabber11/laravel-tip-wherehas-or-whererelation-240d</guid>
      <description>&lt;p&gt;Welcome to my Laravel tips and tricks. We are going to learn about eloquent usage and its limitation with strength.&lt;/p&gt;

&lt;p&gt;Scenario:&lt;br&gt;
I have user and order table from where i need to pull those user orders which status is “completed”. Before Laravel 8, We used “whereHas” to compare any column value from related tables. But things were getting easier when Laravel 8 introduced “whereRelation” eloquent relation first.&lt;/p&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%2F3wznip77v5weuoh5dcss.jpg" 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%2F3wznip77v5weuoh5dcss.jpg" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wow! Pretty shorter and easy syntax! 😯&lt;/p&gt;

&lt;p&gt;Facts and Limitations:&lt;br&gt;
Sometimes we think, whereRelation has better performance than whereHas. But we are wrong! While converting eloquent to SQL, I found both execute same query:&lt;/p&gt;

&lt;p&gt;/-- whereHas query --/&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from `users` where exists 
  (
    select * from `orders` 
    where `users`.`id` = `orders`.`created_by` 
    and `status` = ? 
    and `orders`.`deleted_at` is null
  ) 
and `users`.`deleted_at` is null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;/-- whereRelation query --/&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from `users` where exists 
  (
    select * from `orders` 
    where `users`.`id` = `orders`.`created_by` 
    and `status` = ? 
    and `orders`.`deleted_at` is null
  ) 
and `users`.`deleted_at` is null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, It’s up to you that what you should use in where because whereRelation has limitation. When you want to check more than one condition you have to use “whereHas” because “whereRelation” only used for related single condition. For multiple condition whereHas use iterative condition in one subquery where whereRelation iterative subquery for each condition check in related table which isn’t totally feasible! 😒&lt;/p&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%2F45cjlnzht74kgd6op41i.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%2F45cjlnzht74kgd6op41i.png" alt="Image description" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, think before you apply eloquent method for code maintainability. Happy Coding!💻&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>mysql</category>
    </item>
  </channel>
</rss>
