<?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>Forem: Shili Mrawen</title>
    <description>The latest articles on Forem by Shili Mrawen (@marwenshili).</description>
    <link>https://forem.com/marwenshili</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%2F748907%2F2745d0a6-b73a-44e8-878d-eb1c9028ea08.jpeg</url>
      <title>Forem: Shili Mrawen</title>
      <link>https://forem.com/marwenshili</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/marwenshili"/>
    <language>en</language>
    <item>
      <title>Integrating CASL with React for Robust Authorization</title>
      <dc:creator>Shili Mrawen</dc:creator>
      <pubDate>Tue, 03 Sep 2024 13:57:50 +0000</pubDate>
      <link>https://forem.com/marwenshili/integrating-casl-with-react-for-robust-authorization-5ci</link>
      <guid>https://forem.com/marwenshili/integrating-casl-with-react-for-robust-authorization-5ci</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Authorization is a critical aspect of any web application, ensuring that users only have access to the features and data they are allowed to interact with. CASL (stands for "Capability-based Access Control") is a popular JavaScript library for handling this logic in a flexible and declarative way. In this article, we’ll walk through how to integrate CASL with a React application, providing you with the tools to implement effective authorization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before diving into the integration, you should be familiar with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of React.&lt;/li&gt;
&lt;li&gt;Familiarity with state management in React.&lt;/li&gt;
&lt;li&gt;Basic knowledge of JavaScript ES6+.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Setting Up CASL
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm install @casl/ability @casl/react&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Defining Abilities
&lt;/h2&gt;

&lt;p&gt;Abilities define what actions a user can perform on particular resources. Let’s start by creating an ability instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Ability } from '@casl/ability';

const defineAbilitiesFor = (user) =&amp;gt; {
  return new Ability([
    {
      action: 'read',
      subject: 'Article',
    },
    {
      action: 'update',
      subject: 'Article',
      conditions: { authorId: user.id },
    },
  ]);
};

export default defineAbilitiesFor;

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

&lt;/div&gt;



&lt;p&gt;In this example, we define two abilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All users can read articles.&lt;/li&gt;
&lt;li&gt;Users can only update articles they authored.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3: Integrating CASL with React
&lt;/h2&gt;

&lt;p&gt;To use these abilities in your React components, you can create a context to provide the ability instance throughout your app.&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, { createContext, useContext } from 'react';
import { Ability } from '@casl/ability';

const AbilityContext = createContext();

export const AbilityProvider = ({ children, user }) =&amp;gt; {
  const ability = defineAbilitiesFor(user);

  return (
    &amp;lt;AbilityContext.Provider value={ability}&amp;gt;
      {children}
    &amp;lt;/AbilityContext.Provider&amp;gt;
  );
};

export const useAbility = () =&amp;gt; useContext(AbilityContext);

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Protecting Components
&lt;/h2&gt;

&lt;p&gt;Now that you’ve set up the context, you can protect your components using the Can component provided by @casl/react.&lt;br&gt;
&lt;/p&gt;

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

function Article({ article }) {
  const ability = useAbility();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{article.title}&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;{article.content}&amp;lt;/p&amp;gt;

      &amp;lt;Can I="update" a="Article"&amp;gt;
        &amp;lt;button&amp;gt;Edit Article&amp;lt;/button&amp;gt;
      &amp;lt;/Can&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Here, the "Edit Article" button will only be visible if the user has permission to update the article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Handling Unauthorized Access
&lt;/h2&gt;

&lt;p&gt;CASL can also help manage what happens when a user attempts an unauthorized action. This can be done by checking abilities in event handlers or API calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleEdit = () =&amp;gt; {
  if (!ability.can('update', article)) {
    alert('You are not allowed to edit this article!');
    return;
  }

  // proceed with editing logic
};

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Integrating CASL with React provides a clean and declarative way to manage authorization in your applications. By defining abilities and using the Can component, you can easily control what users can see and do, improving both the security and user experience of your app.&lt;/p&gt;

</description>
      <category>casl</category>
      <category>authorization</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Mastering Advanced React: Strategies for Efficient Rendering and Re-Rendering</title>
      <dc:creator>Shili Mrawen</dc:creator>
      <pubDate>Tue, 03 Sep 2024 13:39:40 +0000</pubDate>
      <link>https://forem.com/marwenshili/mastering-advanced-react-strategies-for-efficient-rendering-and-re-rendering-390p</link>
      <guid>https://forem.com/marwenshili/mastering-advanced-react-strategies-for-efficient-rendering-and-re-rendering-390p</guid>
      <description>&lt;p&gt;In this section, we'll dive into the concept of re-rendering in React, exploring what triggers a re-render and why it's crucial to understand this process. &lt;/p&gt;

&lt;p&gt;We'll also look at some common problems developers face with unnecessary or frequent re-renders, which can lead to performance issues. Finally, I'll share practical tips on how to optimize re-renders in your React applications, ensuring a smoother and more efficient user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  React LifeCycle
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zuxsrx2tnjm2t3tkgle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zuxsrx2tnjm2t3tkgle.png" alt="Re-rendering in React and exploring what triggers a re-render and why it's crucial to understand this process." width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;A state update can trigger &lt;strong&gt;unnecessary re-renders&lt;/strong&gt; of unrelated components ..&lt;/p&gt;

&lt;p&gt;The App component manages a piece of state (isOpen) using the useState hook. When the state changes (e.g., when the user clicks the button to open a dialog), React re-renders the entire App component. As a result, components like &lt;strong&gt;VerySlowComponent&lt;/strong&gt;, &lt;strong&gt;BunchOfStuff&lt;/strong&gt; , and &lt;strong&gt;OtherStuffAlsoComplicated&lt;/strong&gt;  which are unrelated to the isOpen state get re-rendered unnecessarily. This can lead to performance inefficiencies, especially if these components are complex or slow to render.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Moving state down&lt;/strong&gt; is an effective solution for preventing unnecessary re-renders of unrelated components in React. The concept involves moving the state that controls specific behavior or rendering to the closest common ancestor of only the components that actually need that state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;ModalOpener /&amp;gt;
      &amp;lt;VerySlowComponent /&amp;gt;
      &amp;lt;BunchOfStuff /&amp;gt;
      &amp;lt;OtherStuffAlsoComplicated /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

function ModalOpener() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;Button onClick={() =&amp;gt; setIsOpen(true)}&amp;gt;Open dialog&amp;lt;/Button&amp;gt;
      {isOpen ? &amp;lt;ModalDialog onClose={() =&amp;gt; setIsOpen(false)} /&amp;gt; : null}
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5704nuhdqj5wfo5mu8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5704nuhdqj5wfo5mu8o.png" alt="Moving State Down" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The isOpen state is moved into the ModalOpener component, which is only responsible for rendering the button and the modal. Now, when isOpen changes, only ModalOpener and its children re-render, leaving the other components (VerySlowComponent, BunchOfStuff, OtherStuffAlsoComplicated) untouched.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This approach optimizes performance by ensuring that only the components that actually need to re-render will do so, avoiding unnecessary rendering of unrelated components.&lt;/p&gt;

</description>
      <category>react</category>
      <category>rendering</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
