<?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 Ravi Dhiman on Medium]]></title>
        <description><![CDATA[Stories by Ravi Dhiman on Medium]]></description>
        <link>https://medium.com/@ravid7000?source=rss-c5c593ebf8b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*MSvpeLwMnGVUnq8VoqeYgw.jpeg</url>
            <title>Stories by Ravi Dhiman on Medium</title>
            <link>https://medium.com/@ravid7000?source=rss-c5c593ebf8b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:13:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ravid7000/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[How Did I Reduced Sentry Quota Utilization: A Practical Guide]]></title>
            <link>https://ravid7000.medium.com/how-did-i-reduced-sentry-quota-utilization-a-practical-guide-af0b4f0cebd2?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/af0b4f0cebd2</guid>
            <category><![CDATA[sentry]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[quota]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Tue, 07 Oct 2025 13:21:01 GMT</pubDate>
            <atom:updated>2025-10-07T13:21:01.029Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P_PpjPBJJsUZkTX4WPU-jA.png" /></figure><p>If you’re using Sentry for error monitoring, you’ve probably encountered quota limits at some point. Whether you’re on a free tier or a paid plan, managing your event volume is crucial for staying within budget while maintaining effective error tracking. In this guide, I’ll share proven strategies to reduce your Sentry quota utilization without compromising on meaningful insights.</p><h3>1. Understanding Sentry Quotas</h3><p>Before diving into optimization strategies, it’s important to understand what counts against your quota. Sentry tracks events, which include errors, transactions, and performance monitoring data. Each event type contributes to your overall usage, and different plans have different limits.</p><h3>2. Technical Architecture</h3><h4>2.1 Implementation Layers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*W54teXY8kuUmLdxEDln_8A.png" /></figure><h4>2.2 Data Flow Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KTHby_yvaI-FTnwuGuGjrA.png" /></figure><h3>3 Implementation Plan</h3><h4>3.1 Server Side (High Impact)</h4><h4>3.1.1 Enable Spike Protection ✅</h4><p>A spike is a significant, temporary increase in your event volume. Because Sentry bills based on the number of events sent monthly, spikes can quickly consume your quota for an entire month. Spike Protection guards against this by establishing a spike threshold for the average number of events each of your projects sends and then dropping them after that threshold is reached.</p><ul><li>Spike Protection currently applies to errors, transactions or spans, and attachments.</li><li>Spike Protection can be enabled on a per-project basis</li></ul><p><strong>Implementation Steps:</strong></p><ol><li>Navigate to <strong>Settings &gt; Spike Protection</strong></li><li>Click “Enable All” to activate for all projects</li></ol><p><strong>Expected Outcome:</strong> 20–30% reduction in spike-related quota consumption</p><h4>3.1.2 Configure Inbound Filters</h4><p>Inbound data filters allow to determine which errors, if any, Sentry should ignore. These filters are exclusively applied at ingest time and not later in processing.</p><p><strong>Implementation Steps:</strong></p><ol><li>Navigate to <strong>Settings &gt; Projects &gt; [Project] &gt; Processing &gt; Inbound Filters</strong></li><li>Enable pre-defined filters:</li></ol><ul><li>✅ Browser extension errors</li><li>✅ Localhost events</li><li>✅ Legacy browser errors</li><li>✅ Web crawler events</li><li>✅ React hydration errors</li></ul><p><strong>Error Message Filters:</strong></p><ul><li>Navigate to <strong>Settings &gt; Projects &gt; [Project] &gt; Inbound Filters</strong></li><li>Add patterns to “Error Message” field:</li></ul><pre>*network timeout*<br>*connection refused*<br>*CORS*<br>*Script error*<br>*Non-Error promise rejection*</pre><p><strong>Expected Outcome: </strong>10–15% reduction in noise events</p><h4>3.2 SDK Optimization (Low Priority) (Significant Impact)</h4><p>(if not done already) To implement SDK optimizations effectively, we need to establish clear guidelines for Sentry SDK integration. I propose building a unified error-monitoring SDK that encapsulates the Sentry configuration. This approach would enable consistent integration of Sentry or any future monitoring provider across all projects.</p><h4>3.2.1 Implement Sample Rate Configuration</h4><p>Sentry recommends sampling errors and transactions for two reasons:</p><ol><li>Capturing a single trace involves minimal overhead, but capturing traces for <em>every</em> page load or <em>every</em> API request may add an undesirable load to your system.</li><li>Enabling sampling allows us to better manage the number of events sent to Sentry.</li></ol><p>We can find a balance of how many errors and transactions need to be sent to sentry server once we stablize the application.</p><ul><li>This approach can significantly reduce the quota usage</li><li>But we may miss out some of critical errors being ingested to sentry with the sampleRate and traceSampleRate</li></ul><p><strong>JavaScript Implementation:</strong></p><pre>const environment = process.env.NODE_ENV;<br><br>Sentry.init({<br>  dsn: &#39;dsn&#39;,<br>  sampleRate: 1,          // 100% of errors<br>  tracesSampleRate: 0.1,     // 10% of transactions, reduce number of transactions<br>  environment,<br>});<br><br>// or environment specific sampleRate<br><br>Sentry.init({<br>  dsn: &quot;your-dsn&quot;,<br>  environment,<br>  sampleRate: environment === &#39;production&#39; ? 1.0 : 0.1,<br>  tracesSampleRate: environment === &#39;production&#39; ? 0.2 : 0.05,<br>});</pre><h4>3.2.2 Implement beforeSend Filtering</h4><p><strong>JavaScript Implementation:</strong></p><pre>Sentry.init({<br>  beforeSend(event, hint) {<br>    // Filter test environment events<br>    if (event.environment === &#39;test&#39; || event.environment === &#39;development&#39;) {<br>      return null;<br>    }<br><br>    // Filter specific error types<br>    const error = hint.originalException;<br>    if (error instanceof TypeError &amp;&amp; error.message.includes(&#39;Network request failed&#39;)) {<br>      return null;<br>    }<br><br>    // Remove sensitive data<br>    if (event.user) {<br>      delete event.user.email;<br>      delete event.user.ip_address;<br>    }<br><br>    return event;<br>  }<br>});</pre><h4>3.2.3 Implement Ignore Lists for Common Noise</h4><p>Every application has recurring errors that don’t require immediate attention. Use Sentry’s ignoreErrors configuration to filter these out at the SDK level.</p><pre>Sentry.init({<br>  dsn: &quot;your-dsn&quot;,<br>  ignoreErrors: [<br>    &#39;ResizeObserver loop limit exceeded&#39;,<br>    &#39;Non-Error promise rejection captured&#39;,<br>    &#39;AbortError&#39;,<br>    /Extension context invalidated/,<br>    /Load failed/,<br>  ],<br>});</pre><h4>3.2.4 Optimize Transaction Naming</h4><p>Poorly named transactions can cause quota issues. Avoid using dynamic values in transaction names:</p><p>Bad:</p><pre>// Creates a unique transaction for every user ID<br>Sentry.startTransaction({ name: `/user/${userId}` });</pre><p>Good:</p><pre>// Creates a single transaction type<br>Sentry.startTransaction({ name: &#39;/user/:id&#39; });</pre><h4>3.3 Advanced Configuration (Significant Impact)</h4><h4>3.3.1 Configure Project-Level Rate Limits</h4><p><strong>Implementation Steps:</strong></p><ol><li>Navigate to <strong>Settings &gt; Projects &gt; [Project] &gt; SDK Setup &gt; Client Keys (DSN)</strong></li><li>Click “Configure” for primary DSN</li><li>Set rate limits based on historical data:</li></ol><ul><li><strong>Critical Projects:</strong> 1000 events/hour</li><li><strong>Standard Projects:</strong> 500 events/hour</li><li><strong>Development Projects:</strong> 100 events/hour</li></ul><p><strong>Rate Limit Calculation:</strong></p><pre>Daily Maximum ÷ 24 hours ÷ 60 minutes = Events per minute<br>Example: 35,000 ÷ 24 ÷ 60 = ~24 events/minute</pre><h4>3.4. Enterprise Features</h4><h4>3.4.1 Configure Spend Allocation</h4><p>Spend Allocation gives the ability to prioritize most important projects. We can do this by setting aside a set amount of reserved volume (errors and attachments) to be used by those projects each month. This ensures that all the projects that are important, are always monitored, regardless of the volume other projects consume.</p><p><strong>Implementation Steps:</strong></p><ol><li>Navigate to <strong>Settings &gt; Spend Allocations &gt; New Allocation</strong></li><li>Allocate reserved volume by priority:</li></ol><ul><li><strong>Critical Services:</strong> 40% of quota</li><li><strong>User-Facing Apps:</strong> 35% of quota</li><li><strong>Internal Tools:</strong> 15% of quota</li><li><strong>Development:</strong> 10% of quota</li></ul><p><strong>Example Allocation for 200K Event Quota:</strong></p><pre>Critical Services: 80,000 events<br>User-Facing Apps: 70,000 events<br>Internal Tools: 30,000 events<br>Development: 20,000 events</pre><h3>5. Monitor and Adjust Regularly</h3><p>Set up alerts to notify you when you’re approaching your quota limits. Regularly review your Sentry dashboard to identify:</p><ul><li>Top error sources</li><li>Most frequent issues</li><li>Events that could be filtered</li><li>Opportunities for additional sampling</li></ul><h3>6. Conclusion</h3><p>Reducing Sentry quota utilization is about finding the right balance between comprehensive monitoring and efficient resource usage. Start with broad strokes like adjusting sample rates and implementing basic filters, then refine your approach based on your specific needs and usage patterns.</p><p>Remember that the goal isn’t to reduce quota usage to zero — it’s to ensure you’re capturing the errors that matter while filtering out noise. By implementing these strategies thoughtfully, you can maintain excellent observability while staying within your budget.</p><p>Have you found other effective ways to manage Sentry quotas? I’d love to hear about your experiences in the comments below.</p><p>Happy debugging! 🐛</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af0b4f0cebd2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Proxies in React: The Sneaky State Spy You Didn’t Know You Needed]]></title>
            <link>https://ravid7000.medium.com/proxies-in-react-the-sneaky-state-spy-you-didnt-know-you-needed-e9552be63a5e?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9552be63a5e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[proxy]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Sun, 25 Aug 2024 18:10:55 GMT</pubDate>
            <atom:updated>2024-08-25T18:10:55.708Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*okl-rACKVdPy6J2tGdOa6A.jpeg" /></figure><p>Have you heard of Javascript [Proxy](<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy</a>)? I manage to replace useReducer by using it inside React component.</p><p>In this post, I’ll show you how to leverage proxies to seamlessly detect changes in nested state, keep your components updated, and say goodbye to manual deep-cloning and other headaches.</p><h3>What’s the Big Deal About Proxies?</h3><p>JavaScript’s Proxy is like a super-spy that can intercept and customize operations performed on an object, such as getting, setting, or deleting properties. This makes it perfect for listening to state changes, even in nested objects, without the need for deep comparison or unnecessary re-renders.</p><p>Here’s what we’re aiming for:</p><ul><li>Reactive state that updates automatically when any nested property changes.</li><li>No more manual cloning of deeply nested objects to trigger React updates.</li><li>Handling arrays and nested structures with minimal effort.</li></ul><h3>Getting Started: Setting Up the Proxy</h3><p>Before we dive into integrating with React, let’s break down how to set up a proxy that handles nested objects. The idea is simple: wrap your initial state in a proxy that can spy on any changes and trigger an update when needed.</p><p>Here’s a basic example:</p><pre><br>function createProxyState(state, onChange) {<br>  // Check if the state is an object or array; if not, return the state as-is<br>  if (typeof state !== &#39;object&#39; || state === null) {<br>    return state;<br>  }<br><br>  // Create a handler for the proxy<br>  const handler = {<br>    get(target, property) {<br>      const value = target[property];<br><br>      // If the value is an object or array, recursively create a proxy for it<br>      if (typeof value === &#39;object&#39; &amp;&amp; value !== null) {<br>        return createProxyState(value, onChange);<br>      }<br><br>      return value;<br>    },<br><br>    set(target, property, value) {<br>      // Only trigger onChange if the value actually changes<br>      if (target[property] === value) return true;<br><br>      // If the new value is an object or array, recursively proxy it<br>      if (typeof value === &#39;object&#39; &amp;&amp; value !== null) {<br>        value = createProxyState(value, onChange);<br>      }<br><br>      // Set the new value on the target object<br>      target[property] = value;<br><br>      // Trigger the onChange callback<br>      onChange(target);<br><br>      // Indicate success<br>      return true;<br>    },<br>  };<br><br>  // Return a proxy wrapping the state object<br>  return new Proxy(state, handler);<br>}</pre><h3>Integrating the Proxy with React</h3><p>Now comes the fun part — integrating this proxy setup into a React hook! We’ll create a useProxyState hook that wraps our initial state and ensures any changes automatically trigger React re-renders.</p><pre>import { useState, useEffect } from &#39;react&#39;;<br><br>export function useProxyState(initialState) {<br>  const [state, setState] = useState(initialState);<br><br>  useEffect(() =&gt; {<br>    // Create a proxy with the onChange callback to trigger state updates<br>    const proxiedState = createProxyState(state, st =&gt; {<br>      // Trigger a React state update<br>      console.log(&#39;state updated&#39;, st);<br>      setState({ ...proxiedState });<br>    });<br><br>    // Set the state to the proxied state on the first render<br>    setState(proxiedState);<br>  }, []);<br><br>  return state;<br>}</pre><h3>Let’s use it into a react component</h3><pre>import &#39;./proxy&#39;<br><br>export function App(props) {<br>  const state = useProxyState({<br>    name: &#39;Alice&#39;,<br>    details: {<br>      age: 25,<br>      hobbies: [&#39;reading&#39;, &#39;coding&#39;],<br>    },<br>  });<br><br>  const updateName = () =&gt; {<br>    state.name = &#39;Bob&#39;; // The proxy will trigger and update React state automatically<br>  };<br><br>  const addHobby = () =&gt; {<br>    state.details.hobbies.push(&#39;gaming&#39;); // The proxy will handle nested changes too<br>  };<br><br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Name: {state.name}&lt;/h1&gt;<br>      &lt;h2&gt;Age: {state.details.age}&lt;/h2&gt;<br>      &lt;h3&gt;Hobbies:&lt;/h3&gt;<br>      &lt;ul&gt;<br>        {state.details.hobbies.map((hobby, index) =&gt; (<br>          &lt;li key={index}&gt;{hobby}&lt;/li&gt;<br>        ))}<br>      &lt;/ul&gt;<br>      &lt;button onClick={updateName}&gt;Update Name&lt;/button&gt;<br>      &lt;button onClick={addHobby}&gt;Add Hobby&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><h3>Pitfalls and Things to Watch Out For</h3><p>While proxies are powerful, there are some caveats:</p><ul><li>Be mindful of performance in very large or deeply nested objects.</li><li>JavaScript’s Proxy doesn’t work in environments that don’t support it (like older browsers).</li></ul><p>Thanks for reading my post !!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9552be63a5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use SCC patterns to build react components]]></title>
            <link>https://levelup.gitconnected.com/refactor-your-react-big-ugly-component-using-scc-pattern-e0509ef66525?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0509ef66525</guid>
            <category><![CDATA[components]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[refactor]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Fri, 28 May 2021 15:55:34 GMT</pubDate>
            <atom:updated>2021-06-12T04:26:46.636Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yGgecfJjsYKSZ0rZMu0vgA.png" /></figure><p>Hi Developers, In this tutorial we will learn how to refactor React components and make them more maintainable and testable using SCC pattern.</p><p>I’m assuming you’re using Hooks inside component. Which makes things more complicated. Personally, I don’t like React hooks, especially useEffect . But you can’t get rid of it. Alternatively, you can create custom hooks using React hooks to avoid directly dealing with useEffect . For example, you can create useBeforeUpdate useAfterUpdate useAfterUpdateOnce useAfterUpdateWhen , those are for another blog. Today I’ll be focusing on refactoring the components using SCC pattern.</p><h3>What is SCC pattern?</h3><p>SCC stands for State, Controller and Component. It is most popular pattern used by Angular. Let’s see them one by one.</p><h3><strong>State</strong></h3><p>A state is place to store the data. As per SCC pattern rules, there can be only two types of State. One is component state which is private to your component. Other is shared(global) state, which can be shared by multiple components in your application.</p><p>For example,</p><ol><li>Logged-in user details</li><li>UI library theme data</li></ol><p>These should be shared state, so that multiple components can directly access them. You can either store them into React Context or can create custom store to store and update them.</p><h3>Controller</h3><p>A controller is a function which encapsulate all the logic and state to control the component. We will see controller in action in later part.</p><h3>Component</h3><p>A piece of code that build the actual UI. In this article I’m referring component to a React Functional Component.</p><p>Not only React, this SCC pattern you can apply to any UI library. Enough for the explanation, let see the code to refactor.</p><h3>A Todo App</h3><p>I know todo app is very small to be refactored. But this is a good starting point.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/594580199eb889e111a40f327633efac/href">https://medium.com/media/594580199eb889e111a40f327633efac/href</a></iframe><p>App component has a form element to with input field to add new todo and a list to display all the todos. The list also has a remove todo button.</p><p>You can see we are using two states todoList as Array of todos and todoName as String to store new todo name and it has 4 actions addTodo removeTodo toggleTodo changeTodoName</p><p>Now, we have analyse our component’s state and actions let’s refactor them using Controller</p><h3>useController Hook</h3><p>Let’s create a custom hook for the controller and save it in a file named App.controller.js You can use this naming convention to create specific controller for the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30bc51d38bc4df8f827579ba2b1efa2f/href">https://medium.com/media/30bc51d38bc4df8f827579ba2b1efa2f/href</a></iframe><p>We have moved the state and actions inside this hook and we are returning only state and actions from this hook, because that is the rule.</p><p>Additionally we have added two more states doneTodo and undoneTodo which are derived from todoList state. You can see how we used useMemo hook to derived state.</p><h3>Refactor Component</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/650c317b2ed781d2e6d1cbe2070cc662/href">https://medium.com/media/650c317b2ed781d2e6d1cbe2070cc662/href</a></iframe><p>This useTodoController is returning only things state and actions . We have destructed them. But don’t destructed too much. Don’t destructed state and actions further.</p><p><strong>TIP: </strong>Pass component props directly to useTodoController to use them inside your controller.</p><pre>const App = (props) =&gt; {<br>  const { state, actions } = useTodoController(props)<br>}</pre><p>Hope you enjoyed reading this blog. Smash the clap button if you like it and to keep me motivated to write more similar blogs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0509ef66525" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/refactor-your-react-big-ugly-component-using-scc-pattern-e0509ef66525">How to use SCC patterns to build react components</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a React Hook and Publish to NPM Packages]]></title>
            <link>https://levelup.gitconnected.com/build-a-react-hook-and-publish-to-npm-packages-807041135982?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/807041135982</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Wed, 24 Feb 2021 07:37:19 GMT</pubDate>
            <atom:updated>2021-02-25T06:57:57.529Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Plf61ZFWxtrBvbaF7Xpq5w.png" /><figcaption>react-hook-typescript</figcaption></figure><p>This is a small tutorial for how to build a custom React custom hook.</p><p>React hooks are more popular now with functional components and provides a way to use React state and lifecycles. One can convert component business logic to a custom hook. It will make your code cleaner and manageable. Let’s see how.</p><p>There is two core React hooks:</p><ul><li><strong>useState</strong></li><li><strong>useEffect</strong></li></ul><h3>useState Hook</h3><p>React’s useState home is used to create a state in the functional component. React will preserve this state between re-renders. useState returns a pair: the current state value and a function that lets you update it. You can call this function from an event handler or somewhere else. It’s similar to this.setState in a class, except it doesn’t merge the old and new state.</p><h3>useEffect</h3><p>The effect hook adds the ability to handle side effects and mimic the lifecycle of react similarly to class-based components. It serves the same purpose as <strong>componentDidMount</strong>, <strong>componentDidUpdate</strong>, and <strong>componentWillUnmount</strong> in React classes, but unified into a single API. When you call useEffect, you’re telling React to run your “effect” function after flushing changes to the DOM.</p><h3>Building Your Own Hook 💡</h3><p>We will build a custom hook to use browser storages with an option to use localStorage and sessionStorage for this tutorial.</p><blockquote>useBrowserStorage</blockquote><p>If a function name starts with “use” then it is called a hook. The <em>useBrowserStorage</em> naming convention is how linter plugin is able to find bugs in the code using Hooks.</p><h3>Setup</h3><p>The quickest way to start, by using a boilerplate. So that you don’t need to worry about adding dependencies and build tools. We will be using create-react-hook NPM package to boilerplate the project.</p><p><a href="https://www.npmjs.com/package/create-react-hook">https://www.npmjs.com/package/create-react-hook</a></p><pre>npx create-react-hook &lt;hook-name&gt;</pre><p>After running this command answers some question and the CLI will setup the project.</p><h3>Write Your Logic</h3><p>Now you have come this far, let’s write our useBrowserStorage hook logic. If you have setup the project using create-react-hook CLI then there is an entry file in src/index.(jsx, tsx) which has some example code to write hook. Let’s replace it.</p><pre>export const useBrowserStorage = () =&gt; {</pre><pre><em>  // logic will go here</em></pre><pre>}</pre><p>We will provide one option { type } to switch between localStorage and sessionStorage . Default value for type is localStorage .</p><pre>const STORAGE_TYPES = [&#39;localStorage&#39;, &#39;sessionStorage&#39;];</pre><pre>export const useBrowserStorage = (options = {}) =&gt; {</pre><pre>  const type = (STORAGE_TYPES.includes(options.type) &amp;&amp; options.type) || &#39;localStorage&#39;;</pre><pre>}</pre><p>Now let’s check if browser supports storages. It will also be checked if our hook is run on server side then it will not generate any error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c07bfd2d6534165bf88cb75f8e14e0f/href">https://medium.com/media/5c07bfd2d6534165bf88cb75f8e14e0f/href</a></iframe><p>Finally combined together in one file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c5698d8aba868da4704504533eae2ad/href">https://medium.com/media/0c5698d8aba868da4704504533eae2ad/href</a></iframe><h3>Publish To NPM</h3><p>Once we are ready with our hook, let’s publish it to NPM packages.</p><p>Login to NPM CLI. Enter your username, password and email to login.</p><pre>npm login</pre><p>Once logged-in successfully, let’s run this command to publish</p><pre>npm publish</pre><p>That’s it!!!</p><p><strong>Tip:</strong> if you want to publish you package again with new changes or bug fixes. Then you have to update your package version. You can manually update the package version into package.json file or you can run this command:</p><pre>npm version patch -m &quot;message&quot;</pre><p><a href="https://docs.npmjs.com/cli/v6/commands/npm-version">Read more</a> for npm version command</p><p>Here is the full source code of useBrowserStorage</p><ul><li><a href="https://github.com/ravid7000/use-browser-storage">ravid7000/use-browser-storage</a></li><li><a href="https://www.npmjs.com/package/use-browser-storage">use-browser-storage</a></li></ul><p>Hope you like this tutorial. Thank you for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=807041135982" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/build-a-react-hook-and-publish-to-npm-packages-807041135982">Build a React Hook and Publish to NPM Packages</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Design Patterns to build UI Components  library in React]]></title>
            <link>https://medium.com/cleartax-engineering/design-patterns-to-build-ui-components-library-in-react-f547ebbd0e46?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f547ebbd0e46</guid>
            <category><![CDATA[system-ui]]></category>
            <category><![CDATA[styled-components]]></category>
            <category><![CDATA[ui-component]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 05:22:28 GMT</pubDate>
            <atom:updated>2020-10-15T10:31:24.498Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design patterns to build UI components library in React</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xc1ZDs995zzM_jLsxewUZw.jpeg" /></figure><p>What will we learn through this article?</p><ul><li><strong>Why do we need a UI System?</strong></li><li><strong>Creating UI Design System</strong></li><li><strong>Primary and Secondary Components</strong></li><li><strong>Designing Component APIs</strong></li></ul><h3>Why do we need a UI System?</h3><p>To build <strong>consistent</strong> UI across every part of your application can be daunting. A UI system helps your users <strong>navigate intuitively</strong> and makes it uniform across your product, making them familiar with your <strong>design language.</strong> They help teams by giving them a more structured and guided way to build UI for their products. Let’s create one!</p><h3>Creating a UI Design System</h3><p>The best way to start is by defining styles, colors, spacing, text sizes, etc. Design system is the skeleton for any UI library, which is single source of truth for all the styling rules.</p><p>To create a design rule system for React library we will use Styled-Components. But Design System for components is not limited to styled-components, we can also do with CSS variables or other styling library. In this case we will be using styled-components only.</p><p>Let create rules for styling:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e4ccffa968ffb840bf88d3471d2eb0e/href">https://medium.com/media/2e4ccffa968ffb840bf88d3471d2eb0e/href</a></iframe><p>We have defined high level configuration for styling. Let’s see one by one.</p><h4>Colors</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14ceb9537cc3925241b1133ed9df5d8d/href">https://medium.com/media/14ceb9537cc3925241b1133ed9df5d8d/href</a></iframe><h4>Typography</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97ac9427010fd08ef779eccad1058068/href">https://medium.com/media/97ac9427010fd08ef779eccad1058068/href</a></iframe><p>Along with the headings and body text styles, we can also define button, input, etc text styles.</p><h4>Spacing</h4><p>Let’s create a function for spacing. The size of spacing is multiple of 4.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/652e8d3204e064a29c65e5d453e076bc/href">https://medium.com/media/652e8d3204e064a29c65e5d453e076bc/href</a></iframe><h4>Z-Index</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79b0c51429c09d04d55815a34be6c704/href">https://medium.com/media/79b0c51429c09d04d55815a34be6c704/href</a></iframe><h4>Breakpoints</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88ef2a140b84e74f1709b59cf9f3bf6b/href">https://medium.com/media/88ef2a140b84e74f1709b59cf9f3bf6b/href</a></iframe><p>We are using standard screen sizes as our breakpoints. We can define more breakpoints depending on our use case.</p><p><a href="https://styled-components.com/docs/advanced">See this documentation</a> to use the theme in styled-components.</p><h3>Primary and Secondary Components</h3><p>Let’s look at what are primary and secondary components for an UI library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DedoPjLhjrUvWeF6b7citQ.png" /><figcaption>five distinct stages</figcaption></figure><p>The components at atomic level are our <strong>primary components</strong>. For example, inputs, buttons, img, headings. The components which can be build by assembling these atomic components are <strong>secondary components.</strong></p><p>And sometimes we need a component to switch between several visual states, depending on certain conditions. Meaning you need the component to change, perhaps, color to convey different information. For example:</p><ul><li>A button can be normal, primary, secondary, disabled, etc.</li><li>A list item can be selected or not selected.</li><li>A form element can be required, with error or normal.</li></ul><p>A good example of this visual variant pattern is <a href="https://getbootstrap.com/docs/3.4/css/#buttons">Bootstrap’s button styles</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*wS0tHyWNvpTQxZxl3elTdg.png" /><figcaption>Button Component Variant</figcaption></figure><h3>Designing Component APIs</h3><p>React component APIs are the component props. Let us standardise props for all the components. There can be two types of props:</p><ol><li>Global/Shared props</li><li>Component specific props</li></ol><p><strong>Global/Shared props</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4001bb007dc0e743dc08b3d422b0de00/href">https://medium.com/media/4001bb007dc0e743dc08b3d422b0de00/href</a></iframe><p><strong>Component specific props: </strong>Let’s create a button component using base element</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89cc3dc9582b7ffc639f4226059968c1/href">https://medium.com/media/89cc3dc9582b7ffc639f4226059968c1/href</a></iframe><p>Similarly, we can create more variants for button, like ErrorButton, InfoButton, SuccessButton by extending BaseButton component.</p><p>There are no hard rules for defining component APIs. But those should be well defined and developer friendly. All shared props should be applicable on almost all the components.</p><p>In Cleartax, we built an UI library named as Zoids, to avoid custom styling and to achieve consistent UI across products.</p><p><strong>Why do we need to build an UI library when there are lots of open source libraries available?</strong><br>If you’re a startup, a single developer, or if the project is not too complex then I recommend picking an existing component library. However, at some point, you might re-evaluate whether it makes sense to create your component library when you find that existing component libraries don’t fit your needs anymore.</p><p>For Cleartax with complex UI products, building our own library make more sense. We used a variant based component design system, which helps resolve our need for atomic level components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f547ebbd0e46" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cleartax-engineering/design-patterns-to-build-ui-components-library-in-react-f547ebbd0e46">Design Patterns to build UI Components  library in React</a> was originally published in <a href="https://medium.com/cleartax-engineering">ClearTax Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS Selectors and Pseudo Elements]]></title>
            <link>https://ravid7000.medium.com/css-selectors-and-pseudo-elements-81ae3f4a786b?source=rss-c5c593ebf8b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/81ae3f4a786b</guid>
            <category><![CDATA[css-specificity]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[css-selectors]]></category>
            <category><![CDATA[css-pseudo-elements]]></category>
            <dc:creator><![CDATA[Ravi Dhiman]]></dc:creator>
            <pubDate>Fri, 08 May 2020 18:03:32 GMT</pubDate>
            <atom:updated>2020-05-08T18:03:32.856Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UQ7wAF2Ew1L6VZ0yL1WGJg.jpeg" /></figure><p>Today we will learn css <strong>selectors</strong>, <strong>pseudo elements</strong> and master them. While using selectors we should know the <strong>specificity</strong> and <strong>precedence </strong>of element selectors. You can read more from this <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity">Mozilla article</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oKBnnPcp3ATKEVTlAfBdrQ.png" /><figcaption>Source: Stack Overflow. Visualising the <strong>specificity </strong>of selectors.</figcaption></figure><p>Let’s get started!</p><h3>Selectors</h3><ul><li>Class</li><li>Id</li><li>Tag Name</li><li>*</li><li>Attribute</li><li>Child</li><li>Immediate Child</li><li>Sibling</li><li>Pseudo</li></ul><h3>Class Selectors &lt;.&gt;</h3><p>Select elements using class name. It is also refers as dot “.” selector.</p><pre>.dropdown {<br>  position: relative;<br>}</pre><pre>a.link {<br>  text-decoration: none;<br>}</pre><p>Class selectors are basically used to create reusable styles. You can use same class name on more than one element.</p><h3>ID Selectors &lt;#&gt;</h3><p>Select element using id. It is also refers as hash “#” selector. It has more specificity and has higher precedence than any other selectors. It is used to style single element.</p><pre>#top {<br>  <em>display</em>: none;<br>}</pre><pre>#main {<br>  <em>width</em>: 600px;<br>}</pre><h3>Tag Name Selector &lt;p&gt;</h3><p>Style one or more elements using tag name. It has least precedence than “class” and “id” selectors. It is used to style elements to look same across all browsers.</p><pre>p {<br>  <em>margin-top</em>: 0;<br>}</pre><pre>a {<br>  <em>text-decoration</em>: none;<br>  <em>color</em>: red;<br>}</pre><h3>Asterisk Selector &lt;*&gt;</h3><p>Select all the elements. It can also select all elements inside another elements. It has least precedence. It is mostly used to apply reset css. To have same styles in all browsers.</p><pre>* {<br>  box-sizing: border-box;<br>}</pre><p>Select all elements inside &lt;div&gt; elements and set their background color to yellow:</p><pre>div * {<br>  background-color: yellow;<br>}</pre><h3>Attribute Selector &lt;[attribute]&gt;</h3><p>It is used to select all the elements using attribute name. Select all &lt;a&gt; which has “href” attribute and apply yellow background color.</p><pre>a[href] {<br>  color: yellow;<br>  cursor: pointer;<br>}</pre><p><strong>Select by attribute value [<em>attribute</em>=<em>value</em>]:</strong></p><pre>&lt;a target=&quot;_blank&quot;&gt;Open in new tab&lt;/a&gt;</pre><p>Select all &lt;a&gt; which has “target=_blank” attribute.</p><pre>a[target=&quot;_blank&quot;] {<br>  text-decoration: overline;<br>}</pre><p><strong>Select by attribute value contains the word [<em>attribute</em>~=<em>value</em>]:</strong></p><p>Selects all elements with a title attribute containing the word “flower”</p><pre>[title~=&quot;flower&quot;] {<br>  display: inline;<br>}</pre><p><strong>Select by attribute value starts with word [<em>attribute^</em>=<em>value</em>]:</strong></p><p>The [<em>attribute</em>^=<em>value</em>] selector matches every element whose <em>attribute</em> value begins with a specified <em>value</em>. Select all “div” elements whose class name starts with “color”, i.e “color_red”, “colorgreen”</p><pre>div[class^=&quot;color&quot;] {<br>  ...<br>}</pre><h3>Child Selectors</h3><p>Select descendant element of an element. It can select any level deep nested element:</p><pre>.parent .child {<br>  display: block;<br>}</pre><pre>.parent .child .subchild {<br>  display: block;<br>}</pre><pre>p .child {<br>  display: block;<br>}</pre><pre>#main .container {<br>  width: 1100px;<br>}</pre><pre>#main p {<br>  margin-top: 0px;<br>}</pre><h3>Immediate Child Selector</h3><p>Select element which are direct child of parent. It is denoted with greater than “&gt;” sign.</p><pre>parent &gt; child {<br>  css-property;<br>}</pre><p>Consider this html markup. We need to apply padding to the direct child of &lt;ul&gt;.</p><pre>&lt;ul class=&quot;list&quot;&gt;<br>  &lt;li&gt;1&lt;/li&gt;<br>  &lt;li&gt;2<br>   &lt;ul&gt;<br>    &lt;li&gt;a&lt;/li&gt;<br>   &lt;/ul&gt;<br>  &lt;/li&gt;<br>&lt;/ul&gt;</pre><pre>/* CSS */</pre><pre>.list &gt; li {<br> padding-left: 20px;<br>}</pre><p>The above defined rule will be applied to direct child &lt;li&gt; of “.list”, will not be applied to nest &lt;li&gt;a&lt;/li&gt;.</p><h3>Sibling Selector</h3><p>Select element that are placed immediately after specified element. It is denoted with plus “+” sign. Select and style every &lt;p&gt; element that are placed immediately after &lt;div&gt; elements:</p><pre>div + p {<br>  margin-top: 0px;<br>}</pre><h3>Pseudo Elements</h3><p><strong>Pseudo element</strong> is a keyword added to a selector that lets you style a specific part of the selected element. For example:</p><pre>p::first-letter {<br>  font-size: 2rem;<br>}</pre><pre>p::first-line {<br>  color: blue;<br>}</pre><p>Let’s see other pseudo elements:</p><p><strong>:active</strong></p><p>To select an active link. A link becomes active when you click on it. The “:active” can be used on all elements, not only link.</p><pre>a:active {<br>  color: blue;<br>}</pre><p><strong>:visited</strong></p><p>To select a visited link.</p><pre>a:visited {<br>  color: gray;<br>}</pre><p>Browsers limits the styles that can be set for a:visited links, due to security issues.</p><p>Allowed styles are:</p><ul><li>color</li><li>background-color</li><li>border-color (and border-color for separate sides)</li><li>outline color</li><li>column-rule-color</li><li>the color parts of fill and stroke</li></ul><p><strong>::after</strong></p><p>This is used to insert something after element using css content property.</p><pre>div::after {<br>  content: &#39;css content&#39;;<br>}</pre><p><strong>::before</strong></p><p>Same as “::after”, but the content is inserted before the element</p><pre>div::before {<br>  content: &#39;css before content&#39;;<br>}</pre><p><strong>:checked</strong></p><p>This selector matches every checked &lt;input&gt; element (only for radio buttons and checkboxes) and &lt;option&gt; element.</p><pre>input[type=&quot;checkbox&quot;]:checked {<br>  ...<br>}</pre><pre>input[type=&quot;radio&quot;]:checked {<br> ...<br>}</pre><pre>option:checked {<br>  ...<br>}</pre><p><strong>:disabled</strong></p><p>Select every disabled input element and apply styles.</p><pre>input:disabled {<br>  opacity: 0.6;<br>  pointer-events: none;<br>}</pre><p><strong>:empty</strong></p><p>Select every element which don’t have children or text nodes. It is also applicable on input elements.</p><pre>p:empty {<br>  background-color: red;<br>}</pre><pre>input:empty {<br>  color: red;<br>  border-color: red;<br>}</pre><p><strong>:hover</strong></p><p>Apply styles when mouse hover on an element.</p><pre>a:hover {<br>  color: blue;<br>}</pre><p><strong>:focus</strong></p><p>Apply styles when element has focused using mouse click or tab.</p><pre>a:focus {<br>  border-bottom: 1px solid red;<br>}</pre><p><strong>:has()</strong></p><p>Select specified element which has the element specified in parameter. Select &lt;div&gt; which has &lt;img&gt; element.</p><pre>div:has(img) {<br>  background-color: red;<br>}</pre><p>These styles will be applied on the &lt;div&gt; element. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has">Read More on has selector</a>.</p><p><strong>:not()</strong></p><p>Select element that is NOT the specified element/selector.</p><pre>:not(p) {<br>  color: red;<br>}</pre><pre>p:not(.paragraph) {<br>  color: blue;<br>}</pre><p><strong>:nth-child(n)</strong></p><p>Select nth of specified element. Select second &lt;p&gt; tag:</p><pre>p:nth-child(2) {<br>  color: yellow;<br>}</pre><p>Select every &lt;p&gt; tag which is multiple of 3:</p><pre>p:nth-child(3n) {<br>  color: yellow;<br>}</pre><p>There are few pseudo element which I did not cover in this article, like:</p><ul><li><a href="https://www.w3schools.com/cssref/sel_nth-last-child.asp">:nth-last-child(<em>n</em>)</a></li><li><a href="https://www.w3schools.com/cssref/sel_nth-of-type.asp">:nth-of-type(<em>n</em>)</a></li><li><a href="https://www.w3schools.com/cssref/sel_root.asp">:root</a></li><li><a href="https://www.w3schools.com/cssref/sel_selection.asp">::selection</a></li><li><a href="https://www.w3schools.com/cssref/sel_target.asp">:target</a></li><li><a href="https://www.w3schools.com/cssref/sel_required.asp">:required</a></li><li><a href="https://www.w3schools.com/cssref/sel_placeholder.asp">::placeholder</a></li><li><a href="https://www.w3schools.com/cssref/sel_link.asp">:link</a></li><li><a href="https://www.w3schools.com/cssref/sel_indeterminate.asp">:indeterminate</a></li></ul><p>Thanks for reading. If you enjoyed this blog post, share it with a friend!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81ae3f4a786b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>