<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>React on Osmar Petry</title><link>https://osmarpetry.dev/tags/react/</link><description>Recent content in React on Osmar Petry</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Wed, 15 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://osmarpetry.dev/tags/react/rss.xml" rel="self" type="application/rss+xml"/><item><title>Optimizing React with Memoization</title><link>https://osmarpetry.dev/blog/react-optimizing-with-memoization/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/react-optimizing-with-memoization/</guid><description>&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;&#10;&lt;p&gt;It’s important to understand the component lifecycle and when to re-render. Some tips to optimize React include:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Use useMemo or memo to memoize expensive calculations or component render.&lt;/li&gt;&#10;&lt;li&gt;Use useCallback to memoize callback functions.&lt;/li&gt;&#10;&lt;li&gt;Use React.PureComponent instead of React.Component for pure components.&lt;/li&gt;&#10;&lt;li&gt;Use shouldComponentUpdate lifecycle method to avoid unnecessary re-renders.&lt;/li&gt;&#10;&lt;li&gt;Avoid using the state for non-essential data that does not affect the render.&lt;/li&gt;&#10;&lt;li&gt;Avoid using too many inline functions and use memoized callback functions instead.&#10;It is important to note that the most important aspect of optimization is to understand the performance bottlenecks in your application and address them accordingly.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="usememo"&gt;useMemo&lt;/h2&gt;&#10;&lt;p&gt;This is a hook that is used to memoize a component&amp;rsquo;s expensive calculations. It helps to reduce the number of re-renders by only recalculating the value when one of its dependencies has changed. It takes two arguments, the first being a calculation function and the second being an array of dependencies. If the dependencies change, the calculation function will be re-executed, otherwise, the previously calculated value is returned, example:&lt;/p&gt;</description></item><item><title>React ForwardRef</title><link>https://osmarpetry.dev/blog/react-forwardref/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/react-forwardref/</guid><description>&lt;p&gt;By default, React does not let a parent component access the DOM node of a child component. &lt;code&gt;forwardRef&lt;/code&gt; breaks that boundary explicitly — the child opts in to exposing its DOM node (or instance) to whoever holds the ref.&lt;/p&gt;&#10;&lt;p&gt;The typical need is imperative control from a parent: focusing an input inside a design-system &lt;code&gt;&amp;lt;Input&amp;gt;&lt;/code&gt; wrapper, scrolling a &lt;code&gt;&amp;lt;List&amp;gt;&lt;/code&gt; to a specific row, or triggering animations on a child element. Without &lt;code&gt;forwardRef&lt;/code&gt;, the parent would need to own the DOM directly, which defeats the purpose of encapsulation.&lt;/p&gt;</description></item><item><title>React.Suspense</title><link>https://osmarpetry.dev/blog/react-suspense/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/react-suspense/</guid><description>&lt;p&gt;&lt;code&gt;React.Suspense&lt;/code&gt; is a boundary component that lets you declaratively specify a fallback UI while its children are &amp;ldquo;suspended&amp;rdquo; — waiting for code to load or, in frameworks like Next.js/Remix, for async data to resolve. It decouples the loading concern from the component that needs the data, keeping each piece focused on its own responsibility.&lt;/p&gt;&#10;&lt;h2 id="why-use-suspense"&gt;Why use Suspense&lt;/h2&gt;&#10;&lt;p&gt;Suspense lets you declaratively handle loading states for code-splitting and async data (with frameworks that support it). It improves first-load performance and keeps your UI logic focused on states that matter.&lt;/p&gt;</description></item><item><title>useRef</title><link>https://osmarpetry.dev/blog/react-useref/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/react-useref/</guid><description>&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; gives you a mutable container — &lt;code&gt;{ current: value }&lt;/code&gt; — that persists for the full lifetime of the component without triggering re-renders when changed. This makes it the right tool for two distinct jobs: holding a reference to a DOM node for imperative operations, and storing any mutable value that your component needs to remember across renders but that should &lt;em&gt;not&lt;/em&gt; drive the UI.&lt;/p&gt;&#10;&lt;p&gt;The key mental model: &lt;code&gt;useState&lt;/code&gt; is for values that should be &lt;em&gt;shown&lt;/em&gt;; &lt;code&gt;useRef&lt;/code&gt; is for values that should be &lt;em&gt;remembered&lt;/em&gt;.&lt;/p&gt;</description></item><item><title>Testing Concepts Notes</title><link>https://osmarpetry.dev/blog/testing-concepts-notes/</link><pubDate>Fri, 15 Jan 2021 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/testing-concepts-notes/</guid><description>&lt;h2 id="bdd"&gt;&lt;strong&gt;BDD&lt;/strong&gt;&lt;/h2&gt;&#10;&lt;p&gt;BDD (Behavior Driven Development) is a method of testing where you use &amp;ldquo;Given&amp;rdquo; for the scenario, &amp;ldquo;When&amp;rdquo; for the environment of the action, and &amp;ldquo;Then&amp;rdquo; for the action.&lt;/p&gt;&#10;&lt;p&gt;Imagine &lt;strong&gt;AutoLot&lt;/strong&gt;, a car dealership chain building an online vehicle marketplace. A product manager, a developer, and a QA engineer sit together and write this scenario in plain English:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Given a customer browsing the SUV category&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;When the customer clicks &amp;#34;Schedule Test Drive&amp;#34; on a 2024 Honda CR-V&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Then the test drive request should appear as &amp;#34;pending&amp;#34; in the customer&amp;#39;s dashboard&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is BDD in practice — the three roles agree on the expected behavior before any code is written. The npm package &lt;code&gt;jest-cucumber&lt;/code&gt; lets you map these Gherkin-style scenarios directly to Jest tests.&lt;/p&gt;</description></item><item><title>Testing Library vs Enzyme</title><link>https://osmarpetry.dev/blog/testing-library-vs-enzyme/</link><pubDate>Fri, 15 Jan 2021 00:00:00 +0000</pubDate><guid>https://osmarpetry.dev/blog/testing-library-vs-enzyme/</guid><description>&lt;h2 id="introduction"&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/h2&gt;&#10;&lt;p&gt;Testing Library is built around a simple philosophy: the more your tests resemble the way your software is used, the more confidence they can give you. Its three core principles are: tests only break when your app breaks (not when implementation details change), you interact with your app the same way as your users, and built-in selectors find elements the way users do to help you write inclusive code.&lt;/p&gt;</description></item></channel></rss>