<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Soufiane El Hamri, blog</title><description>Writing on React, testing and AI engineering.</description><link>https://www.soufiane-elhamri.com</link><language>en</language><item><title>React performance</title><link>https://www.soufiane-elhamri.com/blogs/react-performance</link><guid isPermaLink="true">https://www.soufiane-elhamri.com/blogs/react-performance</guid><description>How to find what makes a React app slow and fix it.</description><pubDate>Fri, 20 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A React app feels slow when the browser has too much code to download, or when React has too much work to do after each change. The browser draws a new frame about every 16 milliseconds. If React takes longer, clicks and typing wait until it finishes.&lt;/p&gt;
&lt;p&gt;Before you fix anything, find the part that is slow. The React Profiler records what happens when you use the page. It shows which components rendered and how long each one took, so you know where the time goes.&lt;/p&gt;
&lt;p&gt;Sometimes the problem is just how much code you send to the browser. The browser has to download it before the page can start. So send only the code the user needs at that moment. A page they haven’t opened yet, or a chart behind a button, can wait. With &lt;code&gt;lazy&lt;/code&gt;, React downloads that part the first time it appears. &lt;code&gt;Suspense&lt;/code&gt; shows a loading message while it arrives.&lt;/p&gt;
&lt;p&gt;React can also waste time on parts of the page that didn’t change. When a component’s state changes, React renders it again, along with every component inside it. Most of those inner components get the same data as before, so rendering them again does nothing useful. Keep state in the component that uses it, so a change only touches the part of the page that needs it. For a slow component, &lt;code&gt;memo&lt;/code&gt; tells React to skip it when its props are the same as last time. The React Compiler adds &lt;code&gt;memo&lt;/code&gt; for you.&lt;/p&gt;
&lt;p&gt;A slow calculation inside a component, like sorting thousands of items, runs again on every render. &lt;code&gt;useMemo&lt;/code&gt; keeps the result and recalculates only when its inputs change. If one run is still too slow, a web worker can do it on a separate thread, so the page keeps responding.&lt;/p&gt;
&lt;p&gt;Long lists are slow for a simpler reason. Each row is cheap, but React still has to render ten thousand of them and add them all to the page. The user only sees a screenful at a time. Windowing renders just those rows and swaps them as the user scrolls.&lt;/p&gt;
&lt;p&gt;When the work still can’t shrink, &lt;code&gt;useDeferredValue&lt;/code&gt; tells React which update can wait. The rule that matters most is to fix the slow render before the re-render, because skipping renders of a slow component only hides the problem. Using &lt;code&gt;memo&lt;/code&gt; or &lt;code&gt;useMemo&lt;/code&gt; everywhere can even make things worse. Both compare values on every render, and on a cheap component that check can cost more than the render it saves. Every optimization adds code to maintain, so measure again after each change.&lt;/p&gt;
</content:encoded><category>React</category><category>React</category><category>Performance</category><category>Code splitting</category><category>Rendering</category><author>Soufiane El Hamri</author></item><item><title>React Suspense</title><link>https://www.soufiane-elhamri.com/blogs/react-suspense</link><guid isPermaLink="true">https://www.soufiane-elhamri.com/blogs/react-suspense</guid><description>How React shows a loading state while data and code arrive.</description><pubDate>Fri, 20 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Anything that loads in the background raises two questions. What does the user see while it loads, and what do they see if it fails? The usual answer is an effect that fetches the data and a few state flags for loading and error. Every component repeats that code, and the page fills up with spinners.&lt;/p&gt;
&lt;p&gt;Suspense moves both questions out of the component. You wrap part of the page in a &lt;code&gt;Suspense&lt;/code&gt; boundary that shows a loading fallback, and in an error boundary that shows a message. What turns Suspense on is a promise that is still pending. A component usually reads one with &lt;code&gt;use&lt;/code&gt;. If the promise hasn’t resolved yet, &lt;code&gt;use&lt;/code&gt; throws it, and that stops the component. React catches it, shows the nearest fallback and renders the component again once the promise resolves. &lt;code&gt;lazy&lt;/code&gt; works the same way with a promise for code, and data libraries built for Suspense do the same for you. If the promise fails, the error boundary takes over. The component itself only handles the case where the data is there.&lt;/p&gt;
&lt;p&gt;Once data is on screen, swapping it for a spinner each time it changes feels like a step back. Wrap the update in &lt;code&gt;startTransition&lt;/code&gt;, and React keeps the old content visible while the new data loads. The &lt;code&gt;isPending&lt;/code&gt; value from &lt;code&gt;useTransition&lt;/code&gt; tells you new data is coming, so you can dim the old content. A search box is different, because the input must show what the user types right away. &lt;code&gt;useDeferredValue&lt;/code&gt; gives the results a copy of the text that can lag behind, while the input stays current.&lt;/p&gt;
&lt;p&gt;Some waits aren’t needed at all. When a user checks off a task, the request almost always succeeds. &lt;code&gt;useOptimistic&lt;/code&gt; shows the new state right away while the request runs. When the request ends, React goes back to the real data, so a failed request undoes the change by itself.&lt;/p&gt;
&lt;p&gt;Images have their own problem. When new data arrives, the browser keeps the old image until the new one downloads, so an old picture sits next to new text. You can load the image through a promise and let it suspend like data. Give the image its own &lt;code&gt;Suspense&lt;/code&gt; boundary, so the new text can appear first. A &lt;code&gt;key&lt;/code&gt; that changes with the image makes React show a placeholder in place of the old picture.&lt;/p&gt;
&lt;p&gt;The biggest trap is a waterfall, where each request waits for the one before it. A component stops at its first &lt;code&gt;use&lt;/code&gt;, so a request written below it only starts when the first one returns. A child also can’t start its requests until its parent has its data. Lazy-loaded code adds one more step, because the code must download before it can ask for anything.&lt;/p&gt;
&lt;p&gt;The rule that matters most is to start every request before you read it. A promise created during render is new on every render, so the request starts again and React keeps showing the fallback. Start requests early, in a click handler, a route loader or a parent component, and keep the promises in a cache keyed by what you asked for. The component then reads the promise that is already on its way, and data the user already saw shows up right away.&lt;/p&gt;
</content:encoded><category>React</category><category>React</category><category>Suspense</category><category>Data fetching</category><category>Transitions</category><author>Soufiane El Hamri</author></item><item><title>Concurrent rendering in React</title><link>https://www.soufiane-elhamri.com/blogs/concurrent-rendering-in-react</link><guid isPermaLink="true">https://www.soufiane-elhamri.com/blogs/concurrent-rendering-in-react</guid><description>How React keeps the page responsive when it has too much to render.</description><pubDate>Mon, 10 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The browser draws the screen about 60 times per second, which gives each frame around 16 milliseconds. If React needs longer than that to render, the browser has to wait. It can’t update the screen or respond to clicks and key presses until React is done, so the page feels frozen.&lt;/p&gt;
&lt;p&gt;The best fix is to give React less work. Render fewer components, make them cheaper, or only render the rows that are visible on screen. Sometimes the work can’t get any smaller, and that’s the case concurrent rendering, added in React 18, is made for.&lt;/p&gt;
&lt;p&gt;The idea is that React doesn’t have to finish everything in one go. It renders a little, pauses so the browser can update the screen and handle what the user did, and then continues. If a more urgent update comes in before React is done, React drops the unfinished work and starts again with the latest state. The page stays responsive even while React is busy.&lt;/p&gt;
&lt;p&gt;React can’t tell which updates are important, so you tell it which ones can wait. Wrap a state update in &lt;code&gt;startTransition&lt;/code&gt; to mark it as not urgent. Sometimes you can’t wrap the update, because the value comes from somewhere else, like a prop from a parent component. In that case, use &lt;code&gt;useDeferredValue&lt;/code&gt;. It keeps showing the previous value while React prepares the new one in the background, then switches to the new value when it is ready.&lt;/p&gt;
&lt;p&gt;A search box with a long list under it is the classic example. The text in the input should always update right away, so leave that state as it is. The list is the part that can wait, so you give it the deferred value.&lt;/p&gt;
&lt;p&gt;One thing to remember: wrap the slow component in &lt;code&gt;memo&lt;/code&gt;. &lt;code&gt;useDeferredValue&lt;/code&gt; makes React render twice, first with the old value and then with the new one. With &lt;code&gt;memo&lt;/code&gt;, React skips the slow component in the first render because nothing changed for it. Without &lt;code&gt;memo&lt;/code&gt;, React renders the slow part twice, and typing gets even slower. If you use the React Compiler, it adds &lt;code&gt;memo&lt;/code&gt; for you.&lt;/p&gt;
</content:encoded><category>React</category><category>React</category><category>Performance</category><category>Concurrent rendering</category><category>INP</category><author>Soufiane El Hamri</author></item><item><title>Playwright in the age of AI</title><link>https://www.soufiane-elhamri.com/blogs/why-your-project-needs-playwright</link><guid isPermaLink="true">https://www.soufiane-elhamri.com/blogs/why-your-project-needs-playwright</guid><description>Why browser tests matter more when AI writes most of the code.</description><pubDate>Mon, 10 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;AI tools can write a feature in minutes. The code compiles and the unit tests pass. That still doesn’t tell you the feature works for the person using it, or that it didn’t break an old feature. When the same AI writes the code and its tests, both carry the same assumptions, so a broken form can pass every test.&lt;/p&gt;
&lt;p&gt;Playwright opens a real browser and uses your app the way a person would. A test follows one flow your users care about, such as signing up or paying, and checks what they should see. Your tests run again on every change and check every feature you wrote a test for, so you can ship knowing the old features still work.&lt;/p&gt;
&lt;p&gt;Browsers are slow, which is why older end-to-end tests failed at random. Before Playwright clicks a button, it waits until the button is ready to receive the click. When a test checks the page, it keeps checking until the expected result shows up or the time runs out. Each test gets a fresh browser context, so one test can’t leave cookies or data behind for the next. When a test fails, the trace viewer replays every step.&lt;/p&gt;
&lt;p&gt;The one rule that matters most is that you decide what each test expects. Every test ends by checking for a result, like a confirmation message after a payment. That check is your definition of a working feature, so read it whenever an agent writes or changes a test. When a test fails, find out why before anyone edits it, because a test that was changed until it passed no longer checks anything.&lt;/p&gt;
</content:encoded><category>Testing</category><category>Testing</category><category>Playwright</category><category>End-to-end testing</category><category>AI agents</category><author>Soufiane El Hamri</author></item></channel></rss>