Mira
735 subscribers
802 photos
25 videos
20 files
263 links
sporadic attempts at cybersec.
personal channel.

files: @mira_files
playlist: @the_coding_playlist
Download Telegram
Clerk is actually litttt. You can setup auth within 20 mins. You may need to use ngrok for local development or testing purposes while using clerk. Ngrok is like a sneak peek for your web stuff. It creates a tunnel from your local server to the internet, so you can share it with others without actually publishing it. Webhooks use ngrok to test and debug your app's communication with external services, like receiving notifications or data updates in real-time. Clerk may require you to set up webhooks for events such as user authentication, password resets, or other user-related actions. Ngrok allows you to expose your local development server to the internet, enabling Clerk to send webhook events to your local environment for testing and debugging. Plus it allows you for realtime data sync.

#clerk #auth #webdev
🔥21😁1
React's Diffing Algorithm: The Magic Behind Virtual DOM Updates

Ever wondered how React manages to efficiently update the real DOM without going bat-shit crazy every time your data changes? It's all thanks to its diffing algorithm, a clever trick that helps keep things smooth and fast.

Think of it like this: Imagine you have a giant Lego set representing your UI. Each Lego brick is a component in your React app. When your data changes, you need to rearrange the Lego bricks to reflect the new state. 

The Naive Approach

You could just tear down the entire Lego structure and rebuild it from scratch every time something changes. This would be REALLY slow and inefficient.

React's Smarter Approach

React uses a virtual DOM, which is a lightweight JavaScript representation of your actual DOM. Instead of directly manipulating the real DOM, React updates this virtual DOM. When something changes, React compares the old virtual DOM with the new one and identifies the minimal set of changes needed to update the real DOM.

The Diffing Algorithm in Action

Let's break down the process:

1. Reconciliation: When a component's props or state change, React triggers a reconciliation process. It takes the new virtual DOM and compares it with the old one to figure out what's different.

2. Tree Traversal: React uses a clever tree traversal algorithm to find the changes. It works by comparing the elements in the old and new virtual DOMs from top to bottom, comparing their types and properties.

3. Difference Detection: If it finds differences, React applies a set of rules to figure out what kind of update needs to be done. It might need to:
    - Add new elements: Create new DOM nodes.
    - Remove elements: Delete existing DOM nodes.
    - Update existing elements: Change attributes or text content.
    - Re-order elements: Move DOM nodes within the tree.

Code Example

const MyComponent = ({ name }) => {
  return (
    <div>
      <h1>Hello, {name}!</h1>
    </div>
  );
};

// Imagine this component is rendered with name="Yohanestz"
// Then, the name changes to "Sakazuki"

// React will do the following:
// 1. Create a new virtual DOM with name="Sakazuki"
// 2. Compare the new virtual DOM with the old one.
// 3. Detect that the text content of the <h1> element needs to be updated.
// 4. Update only the text content of the <h1> element in the real DOM.


Key Advantages

Performance: React's diffing algorithm is highly efficient, reducing the number of DOM manipulations and resulting in smooth UI updates.

Ease of Development: It simplifies development by abstracting away the complexity of DOM manipulation.

Maintainability: React's declarative style and diffing algorithm help create a more predictable and maintainable codebase.

Final Thoughts

React's diffing algorithm is the secret sauce behind its performance and ease of use. By intelligently comparing virtual DOMs, it helps React efficiently update the real DOM and keep your UI smooth and responsive.

So next time you're building your React app, remember that under the hood, there's a clever algorithm working hard to keep everything running smoothly 😉

#TakeAByte #reactjs #diffing #webdev
@Mi_Ra_Ch
🔥6