<?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>DEV Community: Ravindra Singh</title>
    <description>The latest articles on DEV Community by Ravindra Singh (@ravindrame).</description>
    <link>https://dev.to/ravindrame</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F610412%2Fb7d94a99-04ce-4c14-a440-44bd83322aef.jpeg</url>
      <title>DEV Community: Ravindra Singh</title>
      <link>https://dev.to/ravindrame</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ravindrame"/>
    <language>en</language>
    <item>
      <title>Test Like a User: React Testing Library in One Button</title>
      <dc:creator>Ravindra Singh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 11:15:49 +0000</pubDate>
      <link>https://dev.to/ravindrame/test-like-a-user-react-testing-library-in-one-button-5cmj</link>
      <guid>https://dev.to/ravindrame/test-like-a-user-react-testing-library-in-one-button-5cmj</guid>
      <description>&lt;p&gt;You rename one CSS class. You click around the app and everything works. Then you run your tests, and half of them are red.&lt;/p&gt;

&lt;p&gt;Nothing is broken for your users. Your tests were just looking at the wrong things.&lt;/p&gt;

&lt;p&gt;That's the problem Testing Library solves. &lt;strong&gt;Testing Library is a set of small tools that test your UI the way a person uses it: find things by what they see, do what they do, and check what shows up on the screen.&lt;/strong&gt; React Testing Library is the version for React.&lt;/p&gt;

&lt;p&gt;Let's learn it with one tiny component and one button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meet the button
&lt;/h2&gt;

&lt;p&gt;Here's the whole component we'll test. A heading, an email box and a Subscribe button. When you submit, a thank-you line appears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Subscribe.jsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setDone&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="n"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;setDone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Weekly&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt; &lt;span class="n"&gt;tips&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="n"&gt;htmlFor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"btn-primary"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Subscribe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Thanks&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;subscribing&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, how would &lt;em&gt;you&lt;/em&gt; find that button if you were looking at the page? You wouldn't think "the element with class &lt;code&gt;btn-primary&lt;/code&gt;". You'd think "the button that says Subscribe".&lt;/p&gt;

&lt;p&gt;Hold on to that thought. It's the whole idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two ways to find one button
&lt;/h2&gt;

&lt;p&gt;Here are two tests that both find the same button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Test A: like a user&lt;/span&gt;
&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'button'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Test B: by CSS class&lt;/span&gt;
&lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.btn-primary'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Today, both work. Now watch what happens after a small refactor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F56brmi8pl9nnmm6lvouh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F56brmi8pl9nnmm6lvouh.gif" alt="Test A finds the button by role and survives a refactor, test B breaks" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Test A asks the page what a user would ask
&lt;/h3&gt;

&lt;p&gt;Every element on a page has a &lt;strong&gt;role&lt;/strong&gt;. A &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; has the role &lt;code&gt;button&lt;/code&gt;. An &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; is a &lt;code&gt;heading&lt;/code&gt;. A text &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; is a &lt;code&gt;textbox&lt;/code&gt;. Browsers use these roles to build the &lt;strong&gt;accessibility tree&lt;/strong&gt;, the version of your page that screen readers read out loud.&lt;/p&gt;

&lt;p&gt;Every element can also have an &lt;strong&gt;accessible name&lt;/strong&gt;. For a button, that's its text. For an input, it's the text of its &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;getByRole('button', { name: /subscribe/i })&lt;/code&gt; means: "find the thing a screen reader would call &lt;em&gt;button, Subscribe&lt;/em&gt;." The &lt;code&gt;/subscribe/i&lt;/code&gt; part is a regular expression, so the case of the letters doesn't matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test B asks about the paint job
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.btn-primary&lt;/code&gt; is a styling detail. Users never see class names. Screen readers ignore them.&lt;/p&gt;

&lt;p&gt;So when someone renames the class to &lt;code&gt;.button--main&lt;/code&gt; and wraps the button in a new &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, test B can't find it anymore. It fails. But the app still works perfectly. That's a &lt;strong&gt;false alarm&lt;/strong&gt;, and false alarms teach a team to stop trusting their tests.&lt;/p&gt;

&lt;p&gt;Test A doesn't care. There's still a button called "Subscribe" on the page, so it stays green.&lt;/p&gt;

&lt;p&gt;Testing Library sums this up in one line: &lt;strong&gt;the more your tests resemble the way your software is used, the more confidence they can give you.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 This idea is taught hands-on in the &lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=test-like-a-user" rel="noopener noreferrer"&gt;React course on DevTrail&lt;/a&gt;. You even build a tiny &lt;code&gt;getByRole&lt;/code&gt; yourself, so you see exactly how roles and names are matched. It all runs in your browser, with nothing to install.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There's a bonus, too. If &lt;code&gt;getByRole&lt;/code&gt; can't find your button (say it's a clickable &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; with no name), a screen reader user can't find it either. Your test just found an accessibility bug for free.&lt;/p&gt;

&lt;h2&gt;
  
  
  The query ladder
&lt;/h2&gt;

&lt;p&gt;Testing Library gives you many ways to find things, called &lt;strong&gt;queries&lt;/strong&gt;. Use them roughly in this order, top first:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByRole&lt;/code&gt;&lt;/strong&gt;: a button, link, heading, checkbox, by its name. Your default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByLabelText&lt;/code&gt;&lt;/strong&gt;: form fields, by their label. Great for inputs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByPlaceholderText&lt;/code&gt;&lt;/strong&gt;: only if there is no label (but you should add one).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByText&lt;/code&gt;&lt;/strong&gt;: plain text, like a paragraph or a message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByDisplayValue&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;getByAltText&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;getByTitle&lt;/code&gt;&lt;/strong&gt;: special cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByTestId&lt;/code&gt;&lt;/strong&gt;: last resort, when nothing a user can see will work.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The rule of thumb: if a user can't see it or hear it, it should be your last choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your first complete test
&lt;/h2&gt;

&lt;p&gt;Let's set up a Vite project for testing. Install the tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/user-event @testing-library/jest-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quick tour of what each one does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vitest&lt;/strong&gt; runs your tests (Jest works almost the same way).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;jsdom&lt;/strong&gt; is a pretend browser that lives inside Node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@testing-library/react&lt;/strong&gt; draws your component into that pretend browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@testing-library/user-event&lt;/strong&gt; clicks and types like a real person.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@testing-library/jest-dom&lt;/strong&gt; adds readable checks like &lt;code&gt;toBeInTheDocument()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tell Vitest to use jsdom, and load the extra checks before every test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vite.config.js (add the test block)&lt;/span&gt;
&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'jsdom'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;setupFiles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'./vitest.setup.js'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vitest.setup.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s1"&gt;'@testing-library/jest-dom/vitest'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the test itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Subscribe.test.jsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@testing-library/react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;userEvent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@testing-library/user-event'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Subscribe&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./Subscribe'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'thanks the user after they subscribe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Subscribe&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'a@b.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'button'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;thanks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;npx vitest&lt;/code&gt; and it passes.&lt;/p&gt;

&lt;p&gt;Read it out loud and it sounds like a person using the app:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Show&lt;/strong&gt; the Subscribe form.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type&lt;/strong&gt; an email into the box labelled "Email".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Click&lt;/strong&gt; the button called "Subscribe".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;See&lt;/strong&gt; a thank-you message.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice what the test &lt;em&gt;doesn't&lt;/em&gt; know. It doesn't know about &lt;code&gt;useState&lt;/code&gt;. It doesn't know the state is called &lt;code&gt;done&lt;/code&gt;. It doesn't know any class names. You could rewrite the component with a reducer, a form library or a fetch call, and as long as the user sees the same thing, the test stays green.&lt;/p&gt;

&lt;p&gt;A few details worth knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;userEvent.setup()&lt;/code&gt;&lt;/strong&gt; creates a "user". Its actions are async, so always &lt;code&gt;await&lt;/code&gt; them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;user.type&lt;/code&gt;&lt;/strong&gt; presses every key, one by one, just like real typing. It fires focus, key and input events, not one fake event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;screen&lt;/code&gt;&lt;/strong&gt; is the whole rendered page. You don't need to keep the return value of &lt;code&gt;render&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;findByText&lt;/code&gt;&lt;/strong&gt; waits for the message to appear. Here it shows up right away, but if you later add a real API call, this line keeps working.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  getBy, queryBy or findBy?
&lt;/h2&gt;

&lt;p&gt;Every query comes in three flavours. The difference is what happens when the element isn't there.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flavour&lt;/th&gt;
&lt;th&gt;If nothing matches&lt;/th&gt;
&lt;th&gt;Use it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;getBy...&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Throws an error right away&lt;/td&gt;
&lt;td&gt;The element should be on screen &lt;strong&gt;now&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;queryBy...&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns &lt;code&gt;null&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;You want to check something is &lt;strong&gt;not&lt;/strong&gt; there&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;findBy...&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a promise; keeps retrying, rejects after about 1 second&lt;/td&gt;
&lt;td&gt;The element shows up &lt;strong&gt;later&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So to check that an error message is &lt;em&gt;gone&lt;/em&gt;, use &lt;code&gt;queryBy&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queryByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Each one also has an "All" version, like &lt;code&gt;getAllByRole&lt;/code&gt;, for when you expect several matches.)&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake almost everyone makes: testing the inside
&lt;/h2&gt;

&lt;p&gt;When people start testing React, they reach for the parts they wrote: the state, the class names, the methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 🚨 Tied to the inside of the component&lt;/span&gt;
&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.btn-primary'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBeTruthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email-input'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These tests break when you refactor and stay green when the feature is actually broken. That's the worst of both worlds.&lt;/p&gt;

&lt;p&gt;Two related habits to drop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;getByTestId&lt;/code&gt; for everything.&lt;/strong&gt; Adding &lt;code&gt;data-testid&lt;/code&gt; to every element feels safe, but users can't see test ids. You lose the free accessibility check, too. Keep it for the rare case where nothing visible works.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrapping things in &lt;code&gt;act()&lt;/code&gt; by hand.&lt;/strong&gt; &lt;code&gt;render&lt;/code&gt; and &lt;code&gt;user-event&lt;/code&gt; already do that for you. If you see the warning &lt;em&gt;"An update to Subscribe inside a test was not wrapped in act(...)"&lt;/em&gt;, the fix is usually to &lt;code&gt;await&lt;/code&gt; the user action, or to wait for the final screen with &lt;code&gt;findBy&lt;/code&gt;. Not to add more &lt;code&gt;act&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The simple rule: &lt;strong&gt;test what the user can see and do.&lt;/strong&gt; If a user can't notice the change, your test shouldn't either.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'd learn next
&lt;/h2&gt;

&lt;p&gt;You've written your first real component test. Real apps need a few more tricks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;What Testing Library is, queries, and a first test with user-event&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔒 Mocking API calls in tests, from &lt;code&gt;vi.spyOn(fetch)&lt;/code&gt; to MSW&lt;/li&gt;
&lt;li&gt;🔒 Async UI in depth: loading, empty and error states, &lt;code&gt;waitFor&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;🔒 A custom &lt;code&gt;render&lt;/code&gt; that wraps components in routers, themes and providers&lt;/li&gt;
&lt;li&gt;🔒 Testing custom hooks&lt;/li&gt;
&lt;li&gt;🔒 What to test (and what to skip): a sensible coverage strategy&lt;/li&gt;
&lt;li&gt;🔒 Accessibility in React, so &lt;code&gt;getByRole&lt;/code&gt; finds everything&lt;/li&gt;
&lt;li&gt;🔒 Debugging React apps when a test (or the screen) shows the wrong thing&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Go from zero to hero with React
&lt;/h3&gt;

&lt;p&gt;This post covered &lt;strong&gt;one lesson from Module 17: Testing &amp;amp; Quality&lt;/strong&gt;. The full &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=test-like-a-user" rel="noopener noreferrer"&gt;React: Basic to Advanced&lt;/a&gt;&lt;/strong&gt; course on DevTrail takes you from your first component all the way there: &lt;strong&gt;17 modules and 90 hands-on lessons&lt;/strong&gt;, covering state, Hooks in Depth, How React Renders, Performance, Concurrent React &amp;amp; React 19, Server Components and testing.&lt;/p&gt;

&lt;p&gt;Every lesson has code you run and check &lt;strong&gt;right in your browser&lt;/strong&gt;. No setup, no "works on my machine".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module 1 is free.&lt;/strong&gt; &lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=test-like-a-user" rel="noopener noreferrer"&gt;Start the React course →&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>testing</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React 19 Broke My App: The Changes That Matter</title>
      <dc:creator>Ravindra Singh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 11:14:29 +0000</pubDate>
      <link>https://dev.to/ravindrame/react-19-broke-my-app-the-changes-that-matter-3608</link>
      <guid>https://dev.to/ravindrame/react-19-broke-my-app-the-changes-that-matter-3608</guid>
      <description>&lt;p&gt;I changed one line in &lt;code&gt;package.json&lt;/code&gt;, from React 18 to React 19. The app started. No red errors. Then I noticed every "Save" button on the page was &lt;strong&gt;blank&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The culprit was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Save"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ignored in React 19&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React 19 removes a few old APIs that had been deprecated for years, and adds new ones that make everyday code shorter.&lt;/strong&gt; Most apps need small fixes, not a rewrite. Here's what actually matters, and the two changes worth learning properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will this affect me? A 30-second checklist
&lt;/h2&gt;

&lt;p&gt;Search your code for these. Each "yes" is something to fix:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;code&gt;ReactDOM.render(&lt;/code&gt; or &lt;code&gt;ReactDOM.hydrate(&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;.defaultProps =&lt;/code&gt; on a &lt;strong&gt;function&lt;/strong&gt; component&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;.propTypes =&lt;/code&gt; (React no longer checks them)&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;ref="something"&lt;/code&gt; with a &lt;strong&gt;string&lt;/strong&gt; (string refs)&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;contextTypes&lt;/code&gt; or &lt;code&gt;getChildContext&lt;/code&gt; (legacy context)&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;import { act } from "react-dom/test-utils"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;react-test-renderer/shallow&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] TypeScript ref callbacks written like &lt;code&gt;ref={el =&amp;gt; (box = el)}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All boxes empty? Your upgrade will probably be boring. That's the good kind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrade the safe way: 18.3 first
&lt;/h2&gt;

&lt;p&gt;Don't jump straight to 19. &lt;strong&gt;React 18.3&lt;/strong&gt; is the same as 18.2, plus warnings for everything React 19 removes. So:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;code&gt;react@18.3&lt;/code&gt; and &lt;code&gt;react-dom@18.3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Open your app and tests, and fix every warning in the console.&lt;/li&gt;
&lt;li&gt;Now install React 19.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then let the official codemods do the boring edits for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx codemod@latest react/19/migration-recipe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It rewrites &lt;code&gt;ReactDOM.render&lt;/code&gt;, string refs, the &lt;code&gt;act&lt;/code&gt; import and more. If you use TypeScript, also run &lt;code&gt;npx types-react-codemod@latest preset-19 ./src&lt;/code&gt;. Always read the diff before you commit it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What got removed, and what to use instead
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Old way (React 18)&lt;/th&gt;
&lt;th&gt;React 19&lt;/th&gt;
&lt;th&gt;Use instead&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ReactDOM.render&lt;/code&gt;, &lt;code&gt;ReactDOM.hydrate&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Removed&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;createRoot&lt;/code&gt;, &lt;code&gt;hydrateRoot&lt;/code&gt; from &lt;code&gt;react-dom/client&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;defaultProps&lt;/code&gt; on function components&lt;/td&gt;
&lt;td&gt;Removed&lt;/td&gt;
&lt;td&gt;Default parameters: &lt;code&gt;{ label = "Save" }&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;propTypes&lt;/code&gt; checks&lt;/td&gt;
&lt;td&gt;Silently skipped&lt;/td&gt;
&lt;td&gt;TypeScript&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String refs (&lt;code&gt;ref="input"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Removed&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;useRef&lt;/code&gt; or a ref callback&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Legacy context (&lt;code&gt;contextTypes&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Removed&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;createContext&lt;/code&gt; + &lt;code&gt;useContext&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;act&lt;/code&gt; from &lt;code&gt;react-dom/test-utils&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Moved&lt;/td&gt;
&lt;td&gt;&lt;code&gt;import { act } from "react"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;react-test-renderer/shallow&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removed&lt;/td&gt;
&lt;td&gt;React Testing Library&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The fix for my blank buttons was one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Save"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class components still support &lt;code&gt;defaultProps&lt;/code&gt;. Only function components lost it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two quieter changes that can surprise you
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Errors are no longer thrown twice.&lt;/strong&gt; In React 18, an error during render was re-thrown, which is why you often saw it logged twice in development. In React 19, errors that no error boundary catches go to &lt;code&gt;window.reportError&lt;/code&gt;, and caught ones go to &lt;code&gt;console.error&lt;/code&gt;. If you send errors to a logging service, use the new root options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;onUncaughtError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;componentStack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;onCaughtError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ref callbacks can return a cleanup function.&lt;/strong&gt; React calls it when the element goes away, just like a &lt;code&gt;useEffect&lt;/code&gt; cleanup. Nice! But it means React now cares what your ref callback &lt;em&gt;returns&lt;/em&gt;. In TypeScript, this line is now an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;   &lt;span class="c1"&gt;// returns el: TypeScript complains&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// returns nothing: fine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arrow without braces returns the element, and React would try to treat that as a cleanup. Add braces and the problem is gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  See the two big wins
&lt;/h2&gt;

&lt;p&gt;The rest of React 19 is mostly about &lt;strong&gt;writing less code&lt;/strong&gt;. Two changes do that more than any other. Watch them happen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7vk67egydfwsio422qw2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7vk67egydfwsio422qw2.gif" alt="Before and after: forwardRef turns into a ref prop, and useState plus a submit handler turns into useActionState" width="720" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deep dive 1: &lt;code&gt;ref&lt;/code&gt; is just a prop now
&lt;/h3&gt;

&lt;p&gt;Say you build your own &lt;code&gt;Input&lt;/code&gt; and the parent wants to focus it. In React 18, &lt;code&gt;ref&lt;/code&gt; was special. It never reached your component as a prop, so you had to wrap everything in &lt;code&gt;forwardRef&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;forwardRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;forwardRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In React 19, a function component receives &lt;code&gt;ref&lt;/code&gt; like any other prop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. No wrapper, no import, no strange two-argument function. The parent code doesn't change at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sounds small, but think about a design system with 40 components wrapped in &lt;code&gt;forwardRef&lt;/code&gt;. All of that ceremony just disappears. &lt;code&gt;forwardRef&lt;/code&gt; still works in React 19, so you can remove it slowly, one component at a time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 This is one of the hands-on lessons in the &lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=react-19-changes-that-matter" rel="noopener noreferrer"&gt;React course on DevTrail&lt;/a&gt;. You upgrade a small app to React 19 yourself and watch each change work, right in your browser, with nothing to install.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A cousin of this change: &lt;strong&gt;a context is now its own provider.&lt;/strong&gt; You can write &lt;code&gt;&amp;lt;ThemeContext value="dark"&amp;gt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;ThemeContext.Provider value="dark"&amp;gt;&lt;/code&gt;. The old way still works too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deep dive 2: form Actions and &lt;code&gt;useActionState&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Here's a form most of us have written many times. A newsletter box that shows "Joining..." while it waits, and an error if it fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setIsPending&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setIsPending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// returns an error message, or null&lt;/span&gt;
    &lt;span class="nf"&gt;setIsPending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="n"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;disabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two pieces of state, a handler and &lt;code&gt;preventDefault&lt;/code&gt;. All of it is plumbing.&lt;/p&gt;

&lt;p&gt;React 19 lets you pass a &lt;strong&gt;function&lt;/strong&gt; to &lt;code&gt;&amp;lt;form action&amp;gt;&lt;/code&gt;. React calls it with the form data when the user submits. That function is called an &lt;strong&gt;Action&lt;/strong&gt;. And &lt;code&gt;useActionState&lt;/code&gt; wraps an Action and tracks two things for you: what it returned last, and whether it's still running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useActionState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;submitAction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useActionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prevError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// whatever you return becomes `error`&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// starting value&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;submitAction&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"email"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;disabled&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the hook's result like a sentence: &lt;strong&gt;"the last result, the thing to call, and am I busy?"&lt;/strong&gt; No &lt;code&gt;useState&lt;/code&gt;, no handler, no &lt;code&gt;preventDefault&lt;/code&gt;. As a bonus, React clears the form's fields after the action succeeds.&lt;/p&gt;

&lt;p&gt;Need the pending state inside a child, like a reusable &lt;code&gt;&amp;lt;SubmitButton&amp;gt;&lt;/code&gt;? &lt;code&gt;useFormStatus&lt;/code&gt; from &lt;code&gt;react-dom&lt;/code&gt; reads it from the nearest parent &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;, so you don't have to pass props down.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake almost everyone makes
&lt;/h2&gt;

&lt;p&gt;The function you give to &lt;code&gt;useActionState&lt;/code&gt; gets &lt;strong&gt;two&lt;/strong&gt; arguments, and the form data is the &lt;strong&gt;second&lt;/strong&gt; one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ `form` is actually the previous state here&lt;/span&gt;
&lt;span class="nf"&gt;useActionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ✅ previous state first, form data second&lt;/span&gt;
&lt;span class="nf"&gt;useActionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first argument is the previous result (the &lt;code&gt;null&lt;/code&gt; you started with, then whatever you returned). It's easy to miss because a plain &lt;code&gt;&amp;lt;form action={fn}&amp;gt;&lt;/code&gt; &lt;em&gt;does&lt;/em&gt; get the form data first. If &lt;code&gt;form.get&lt;/code&gt; is "not a function", this is why.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'd learn next
&lt;/h2&gt;

&lt;p&gt;You now know what breaks in React 19 and two patterns that make your code shorter. There's a lot more in the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;The React 19 changes that break apps, and how to fix them&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;✅ &lt;strong&gt;&lt;code&gt;ref&lt;/code&gt; as a prop and form Actions with &lt;code&gt;useActionState&lt;/code&gt;&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔒 &lt;code&gt;useOptimistic&lt;/code&gt;: show the result before the server answers&lt;/li&gt;
&lt;li&gt;🔒 &lt;code&gt;use()&lt;/code&gt;: read a promise or context, even inside an &lt;code&gt;if&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;🔒 &lt;code&gt;useTransition&lt;/code&gt; and &lt;code&gt;useDeferredValue&lt;/code&gt;: keep typing smooth while heavy work runs&lt;/li&gt;
&lt;li&gt;🔒 Suspense for data, and how concurrent rendering really works&lt;/li&gt;
&lt;li&gt;🔒 How React renders: reconciliation, Fiber and why components re-render&lt;/li&gt;
&lt;li&gt;🔒 Server Components and Server Functions: what runs where&lt;/li&gt;
&lt;li&gt;🔒 Performance: memo, &lt;code&gt;useMemo&lt;/code&gt;, &lt;code&gt;useCallback&lt;/code&gt; and when &lt;em&gt;not&lt;/em&gt; to use them&lt;/li&gt;
&lt;li&gt;🔒 Testing React components the right way&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Preparing for interviews? &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/react-interview/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=react-19-changes-that-matter" rel="noopener noreferrer"&gt;React Interview Prep&lt;/a&gt;&lt;/strong&gt; covers React 19 questions too.&lt;/p&gt;




&lt;h3&gt;
  
  
  🚀 Go from zero to hero with React
&lt;/h3&gt;

&lt;p&gt;This post covered a slice of &lt;strong&gt;module 15 of 17&lt;/strong&gt;. The full &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=react-19-changes-that-matter" rel="noopener noreferrer"&gt;React: Basic to Advanced&lt;/a&gt;&lt;/strong&gt; course on DevTrail goes from your first component to hooks, rendering internals, performance, React 19 and Server Components: &lt;strong&gt;17 modules and 90 hands-on lessons&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every lesson has code you run and check &lt;strong&gt;right in your browser&lt;/strong&gt;. No setup, no "works on my machine".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module 1 is free.&lt;/strong&gt; &lt;a href="https://www.devtrail.info/courses/react/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=react-19-changes-that-matter" rel="noopener noreferrer"&gt;Start the React course →&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Two Bank Transfers at Once: Transactions, Explained</title>
      <dc:creator>Ravindra Singh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 11:13:38 +0000</pubDate>
      <link>https://dev.to/ravindrame/two-bank-transfers-at-once-transactions-explained-2l4j</link>
      <guid>https://dev.to/ravindrame/two-bank-transfers-at-once-transactions-explained-2l4j</guid>
      <description>&lt;p&gt;Your account has ₹1,000. Two payments go out at the exact same moment: ₹500 and ₹300. You'd expect ₹200 left. The bank's database says &lt;strong&gt;₹700&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Nobody hacked anything. &lt;strong&gt;Two transfers read the same old balance, each saved its own answer, and the second save wiped out the first.&lt;/strong&gt; That's called a &lt;em&gt;lost update&lt;/em&gt;, and the cure is a database transaction that makes the second transfer wait its turn.&lt;/p&gt;

&lt;p&gt;Let's watch it go wrong, then watch the same scene done safely.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code that looks fine
&lt;/h2&gt;

&lt;p&gt;Here's a withdraw method you'll find in a lot of first Spring Boot projects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orElseThrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// 1. read&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// 2. work it out in Java&lt;/span&gt;
    &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                      &lt;span class="c1"&gt;// 3. write&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read, subtract, save. Run it once and it's perfect. Run it twice &lt;strong&gt;at the same time&lt;/strong&gt; and things get interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch the money disappear
&lt;/h2&gt;

&lt;p&gt;Transfer A wants to send ₹500. Transfer B wants to send ₹300. They arrive together.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdtdaa7iu8fmyxdajuxwi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdtdaa7iu8fmyxdajuxwi.gif" alt="Two transfers read the same balance and one update is lost, then the same scene with a row lock" width="760" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A reads&lt;/strong&gt; the balance: ₹1,000.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B reads&lt;/strong&gt; the balance: still ₹1,000, because A hasn't saved anything yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A saves&lt;/strong&gt; 1,000 − 500 = ₹500.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B saves&lt;/strong&gt; 1,000 − 300 = ₹700.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;B never saw A's change. It did its maths on a number that was already out of date, and then wrote over A's result. The ₹500 payment went out, but the balance never went down by ₹500. The bank just lost money.&lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;race condition&lt;/strong&gt;: the result depends on &lt;em&gt;who gets there first&lt;/em&gt;. Run the same two transfers again and they may come out right, because the timing is different. That's why these bugs pass every test on your laptop and show up on a busy day.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 This is exactly the kind of thing you practise in the &lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=two-bank-transfers" rel="noopener noreferrer"&gt;Spring Boot course on DevTrail&lt;/a&gt;. You write the transactional code yourself and run it right in your browser, with nothing to install.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The fix: one at a time, for this row
&lt;/h2&gt;

&lt;p&gt;The problem is the gap between "read" and "write". Someone else can sneak in there. So we close the gap.&lt;/p&gt;

&lt;p&gt;Look at the second half of the animation. This time Transfer A &lt;strong&gt;locks the row&lt;/strong&gt; while it reads. In SQL that's &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;. It means: "I'm reading this row &lt;em&gt;because I'm about to change it&lt;/em&gt;. Nobody else touches it until I'm done."&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A locks and reads&lt;/strong&gt; ₹1,000.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B asks&lt;/strong&gt; for the same row and has to &lt;strong&gt;wait&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A saves&lt;/strong&gt; ₹500 and commits. The lock is released.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B finally reads&lt;/strong&gt;, and gets the fresh value: ₹500.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B saves&lt;/strong&gt; 500 − 300 = ₹200. ✓&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;B waited a few milliseconds. In exchange, the balance is right.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Spring Boot version
&lt;/h2&gt;

&lt;p&gt;Two small changes. Tell the repository to lock the row, and wrap the method in a transaction so the lock is held from the read all the way to the commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AccountRepository&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LockModeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PESSIMISTIC_WRITE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;// SELECT ... FOR UPDATE&lt;/span&gt;
    &lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"select a from Account a where a.id = :id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByIdForUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TransferService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;AccountRepository&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;TransferService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AccountRepository&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Transactional&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByIdForUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orElseThrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Not enough money"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// No save() needed: the change is written when the transaction commits.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What &lt;code&gt;@Transactional&lt;/code&gt; gives you here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;All or nothing.&lt;/strong&gt; If anything throws, the whole thing rolls back. No half-finished transfers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The lock lives as long as the transaction.&lt;/strong&gt; A row lock is let go on commit or rollback. That's why the transaction has to cover both the read and the write.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's an even shorter fix when the logic is this simple: let the database do the maths in one statement, like &lt;code&gt;update Account a set a.balance = a.balance - :amt where a.id = :id and a.balance &amp;gt;= :amt&lt;/code&gt; (with &lt;code&gt;@Modifying&lt;/code&gt; on the query method). The database locks the row while it updates it, so a second update waits and then works on the new value. If it returns &lt;code&gt;0&lt;/code&gt; rows, there wasn't enough money.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Can't I just raise the isolation level?"
&lt;/h2&gt;

&lt;p&gt;Good question. Every transaction runs at an &lt;strong&gt;isolation level&lt;/strong&gt;, which decides how much it can see of other transactions that are running at the same time. The SQL standard defines four. Here's which classic problems each one blocks:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Isolation level&lt;/th&gt;
&lt;th&gt;Dirty read&lt;/th&gt;
&lt;th&gt;Non-repeatable read&lt;/th&gt;
&lt;th&gt;Phantom read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Uncommitted&lt;/td&gt;
&lt;td&gt;can happen&lt;/td&gt;
&lt;td&gt;can happen&lt;/td&gt;
&lt;td&gt;can happen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;td&gt;can happen&lt;/td&gt;
&lt;td&gt;can happen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;td&gt;can happen (by the standard)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;td&gt;blocked&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In plain words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty read:&lt;/strong&gt; you see another transaction's change before it's committed, and it may still be rolled back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-repeatable read:&lt;/strong&gt; you read the same row twice and get two different values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom read:&lt;/strong&gt; you run the same search twice and new rows have appeared.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Spring you pick a level with &lt;code&gt;@Transactional(isolation = Isolation.REPEATABLE_READ)&lt;/code&gt;. If you don't, you get the database's default: &lt;strong&gt;Read Committed in PostgreSQL&lt;/strong&gt;, &lt;strong&gt;Repeatable Read in MySQL (InnoDB)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now the catch: &lt;strong&gt;the lost update isn't in that table&lt;/strong&gt;, and each database handles it in its own way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At &lt;strong&gt;Read Committed&lt;/strong&gt; (PostgreSQL's default), our read-then-save code loses money. Each read sees committed data, but nothing stops B from saving over A.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;PostgreSQL&lt;/strong&gt;, Repeatable Read &lt;em&gt;does&lt;/em&gt; notice. The second transfer fails with a serialization error ("could not serialize access due to concurrent update"), and your code has to retry it.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;MySQL&lt;/strong&gt;, Repeatable Read (its default) does &lt;em&gt;not&lt;/em&gt; stop it for this code. A plain &lt;code&gt;SELECT&lt;/code&gt; reads an old snapshot, so B still does its maths on ₹1,000.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializable&lt;/strong&gt; prevents it everywhere, but some transactions will be rejected and need a retry, and it costs more under load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a higher isolation level can help, but how it helps depends on your database. A row lock or an atomic &lt;code&gt;UPDATE&lt;/code&gt; says exactly what you mean and behaves the same on both. That's why they're the usual first fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not just databases
&lt;/h2&gt;

&lt;p&gt;The same race happens inside a single Java program. Two threads doing &lt;code&gt;count++&lt;/code&gt;, or both adding to the same &lt;code&gt;ArrayList&lt;/code&gt; or &lt;code&gt;HashMap&lt;/code&gt;, can step on each other in exactly the same way: read, change, write, overwrite. Normal collections like these are &lt;strong&gt;not&lt;/strong&gt; safe to change from several threads at once. Use &lt;code&gt;ConcurrentHashMap&lt;/code&gt;, &lt;code&gt;AtomicInteger&lt;/code&gt;, or a lock.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake almost everyone makes: calling it from the same class
&lt;/h2&gt;

&lt;p&gt;You add &lt;code&gt;@Transactional&lt;/code&gt;, it works in one place, and then you write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TransferService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;payRent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"500"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 🚨 no transaction here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Transactional&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring doesn't add transactions by changing your class. It wraps your bean in a &lt;strong&gt;proxy&lt;/strong&gt;, a stand-in object that starts the transaction and then calls your method. That only happens when the call comes &lt;strong&gt;from outside&lt;/strong&gt;, through the proxy.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;payRent&lt;/code&gt; calls &lt;code&gt;withdraw&lt;/code&gt; directly on &lt;code&gt;this&lt;/code&gt;. The call never passes through the proxy, so the annotation is simply ignored for that call. If nothing else around it is transactional, you have no rollback and your row lock doesn't last until the save.&lt;/p&gt;

&lt;p&gt;The fixes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put &lt;code&gt;@Transactional&lt;/code&gt; on the method that's called from outside (&lt;code&gt;payRent&lt;/code&gt; here), or&lt;/li&gt;
&lt;li&gt;Move &lt;code&gt;withdraw&lt;/code&gt; into a different bean and inject it, so the call goes through the proxy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What you'd learn next
&lt;/h2&gt;

&lt;p&gt;You now know why two transfers can lose money, and one solid way to stop it. Real backends need more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;Race conditions, lost updates and a row-lock fix&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔒 Propagation: &lt;code&gt;REQUIRED&lt;/code&gt;, &lt;code&gt;REQUIRES_NEW&lt;/code&gt;, &lt;code&gt;NESTED&lt;/code&gt;, and what happens when one transactional method calls another&lt;/li&gt;
&lt;li&gt;🔒 Optimistic locking with &lt;code&gt;@Version&lt;/code&gt;, for when waiting on a lock is too costly&lt;/li&gt;
&lt;li&gt;🔒 Deadlocks: two transfers locking rows in opposite order, and how to avoid them&lt;/li&gt;
&lt;li&gt;🔒 Database and Spring Data JPA: entities, relationships, JPQL and migrations&lt;/li&gt;
&lt;li&gt;🔒 Validation and Exception Handling&lt;/li&gt;
&lt;li&gt;🔒 Spring Security&lt;/li&gt;
&lt;li&gt;🔒 Testing Spring Boot Applications&lt;/li&gt;
&lt;li&gt;🔒 Docker and Deployment, then Microservices Fundamentals&lt;/li&gt;
&lt;li&gt;🔒 Real-World Spring Boot Projects you can put on your CV&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Go from zero to hero with Spring Boot
&lt;/h3&gt;

&lt;p&gt;This post covered &lt;strong&gt;one idea from Module 8&lt;/strong&gt;. The full &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=two-bank-transfers" rel="noopener noreferrer"&gt;Learn Spring Boot from Basic to Advanced&lt;/a&gt;&lt;/strong&gt; course on DevTrail takes you through all of it: &lt;strong&gt;13 modules and 86 hands-on lessons&lt;/strong&gt;, including a full lesson on transaction propagation and isolation levels, and ending with real projects.&lt;/p&gt;

&lt;p&gt;Every lesson has code you run and check &lt;strong&gt;right in your browser&lt;/strong&gt;. No setup, no "works on my machine".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module 1 is free.&lt;/strong&gt; &lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=two-bank-transfers" rel="noopener noreferrer"&gt;Start the Spring Boot course →&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>database</category>
      <category>backend</category>
    </item>
    <item>
      <title>Your Spring Boot App Starts in 3 Seconds. Here's How</title>
      <dc:creator>Ravindra Singh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 11:12:46 +0000</pubDate>
      <link>https://dev.to/ravindrame/your-spring-boot-app-starts-in-3-seconds-heres-how-534d</link>
      <guid>https://dev.to/ravindrame/your-spring-boot-app-starts-in-3-seconds-heres-how-534d</guid>
      <description>&lt;p&gt;You click Run. The console fills with lines you don't read. A few seconds later, one line appears that you &lt;em&gt;do&lt;/em&gt; read: &lt;code&gt;Started DemoApplication in 2.9 seconds&lt;/code&gt;. Your API is live.&lt;/p&gt;

&lt;p&gt;So what happened in those 3 seconds? &lt;strong&gt;A Spring Boot application is a normal Java program whose &lt;code&gt;main()&lt;/code&gt; hands control to Spring. Spring then finds your classes, turns them into objects called beans, plugs them together, sets up a web server for you, and starts listening on port 8080.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the whole story. Now let's slow it down, because once you can see these steps, the scary startup errors stop being scary.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tiny app to follow
&lt;/h2&gt;

&lt;p&gt;Here is a small bookshop app. Three classes, all in the package &lt;code&gt;com.example.demo&lt;/code&gt; (or packages below it):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DemoApplication&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DemoApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Dune"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Emma"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;BookService&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;BookController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BookService&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;// Spring passes this in&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/books"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice what's missing. There is no &lt;code&gt;new BookService()&lt;/code&gt; anywhere. There is no server setup. You never tell anyone about port 8080. Hold on to that, we'll see who does all of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch the 3 seconds
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhu0zgnesb1z66y036pdt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhu0zgnesb1z66y036pdt.gif" alt="Spring Boot startup: main(), component scan, beans, auto-configuration, Tomcat, started" width="720" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: &lt;code&gt;main()&lt;/code&gt; hands over the keys
&lt;/h3&gt;

&lt;p&gt;Java doesn't know anything about Spring. It does what it always does: it finds &lt;code&gt;main()&lt;/code&gt; and runs it.&lt;/p&gt;

&lt;p&gt;The only line inside is &lt;code&gt;SpringApplication.run(...)&lt;/code&gt;. From that moment, Spring is in charge. It reads your settings (like &lt;code&gt;application.properties&lt;/code&gt;) and creates the &lt;strong&gt;application context&lt;/strong&gt;. Think of the context as a big box that will hold every object your app needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Component scan makes a guest list
&lt;/h3&gt;

&lt;p&gt;Look at &lt;code&gt;@SpringBootApplication&lt;/code&gt; again. It's really three annotations in one coat:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@ComponentScan&lt;/code&gt;: go and look for my classes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@EnableAutoConfiguration&lt;/code&gt;: set things up based on my dependencies&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@SpringBootConfiguration&lt;/code&gt;: this class holds configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Component scan starts in the package of your main class, &lt;code&gt;com.example.demo&lt;/code&gt;, and walks through &lt;strong&gt;every package below it&lt;/strong&gt;. Any class marked &lt;code&gt;@Component&lt;/code&gt;, &lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Repository&lt;/code&gt;, &lt;code&gt;@Controller&lt;/code&gt; or &lt;code&gt;@RestController&lt;/code&gt; goes on the guest list.&lt;/p&gt;

&lt;p&gt;That last part matters a lot. A class in &lt;code&gt;com.example.demo.web&lt;/code&gt; is found. A class in &lt;code&gt;com.example.mail&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt;, even if it has &lt;code&gt;@Service&lt;/code&gt; on it. It's a sibling, not a child. Keep that in mind for the error section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Beans are built and plugged together
&lt;/h3&gt;

&lt;p&gt;Every class on the guest list becomes a &lt;strong&gt;bean&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what is a bean in Spring Boot?&lt;/strong&gt; Just an object that Spring creates and looks after for you, instead of you calling &lt;code&gt;new&lt;/code&gt;. By default Spring makes one of each and shares it everywhere.&lt;/p&gt;

&lt;p&gt;Now the clever part. Spring looks at each constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BookController(BookService books)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"To build a &lt;code&gt;BookController&lt;/code&gt;, I need a &lt;code&gt;BookService&lt;/code&gt; first." So Spring builds the &lt;code&gt;BookService&lt;/code&gt;, then passes it into the controller's constructor. Dependencies first, then the things that need them. Like building a car: engine before the car.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;dependency injection&lt;/strong&gt;, and using the constructor for it is the recommended way. If your class has only one constructor, you don't even need &lt;code&gt;@Autowired&lt;/code&gt;. Spring uses it automatically.&lt;/p&gt;

&lt;p&gt;Why bother? Because &lt;code&gt;BookController&lt;/code&gt; never decides &lt;em&gt;which&lt;/em&gt; &lt;code&gt;BookService&lt;/code&gt; it gets. In a test, you can hand it a fake one. Your classes stay small and easy to swap.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 This is how the &lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=spring-boot-startup-3-seconds" rel="noopener noreferrer"&gt;Spring Boot course on DevTrail&lt;/a&gt; starts too: you write beans and wire them together, get your code checked right in your browser, and practise reading real startup errors like the ones below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 4: Auto-configuration reads your shopping list
&lt;/h3&gt;

&lt;p&gt;Your &lt;code&gt;pom.xml&lt;/code&gt; has one important line: &lt;code&gt;spring-boot-starter-web&lt;/code&gt;. A &lt;strong&gt;starter&lt;/strong&gt; is a bundle of libraries that usually go together.&lt;/p&gt;

&lt;p&gt;At startup, Spring Boot checks which libraries are on your classpath and sets things up to match:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You added...&lt;/th&gt;
&lt;th&gt;Spring Boot gives you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;spring-boot-starter-web&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Embedded Tomcat, Spring MVC (routing), Jackson (Java ⇄ JSON)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;spring-boot-starter-data-jpa&lt;/code&gt; + a database driver&lt;/td&gt;
&lt;td&gt;A database connection pool and JPA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;spring-boot-starter-validation&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A validator for &lt;code&gt;@Valid&lt;/code&gt; checks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's &lt;strong&gt;auto-configuration&lt;/strong&gt;. It's not magic, it's "if this library is here, and you didn't set it up yourself, here's a sensible default". If you define your own bean of the same kind, Spring Boot steps aside and uses yours.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Tomcat opens the door
&lt;/h3&gt;

&lt;p&gt;In the old days, you installed a server like Tomcat and dropped your app into it. Spring Boot flips that around: &lt;strong&gt;Tomcat lives inside your app&lt;/strong&gt;. Auto-configuration starts it, and it opens port 8080.&lt;/p&gt;

&lt;p&gt;Then you see the line you were waiting for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tomcat started on port 8080 (http)
Started DemoApplication in 2.9 seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;http://localhost:8080/books&lt;/code&gt; and you get &lt;code&gt;["Dune","Emma"]&lt;/code&gt;. Your startup time will be different on your machine, of course. Small apps usually take a few seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to run a Spring Boot application: 3 ways
&lt;/h2&gt;

&lt;p&gt;All three do the same startup you just watched. They differ in &lt;em&gt;when&lt;/em&gt; you'd use them.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Way&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Best for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;IDE run button&lt;/td&gt;
&lt;td&gt;Click ▶ next to &lt;code&gt;main()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Everyday coding and debugging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maven plugin&lt;/td&gt;
&lt;td&gt;&lt;code&gt;./mvnw spring-boot:run&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Running from a terminal without packaging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Executable jar&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;./mvnw package&lt;/code&gt; then &lt;code&gt;java -jar target/*.jar&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Servers, Docker, anything you deploy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A few small notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;./mvnw&lt;/code&gt; is the Maven wrapper that comes with every new project, so you don't need Maven installed. On Windows, use &lt;code&gt;mvnw&lt;/code&gt; instead.&lt;/li&gt;
&lt;li&gt;On Gradle projects, the second one is &lt;code&gt;./gradlew bootRun&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The jar holds &lt;strong&gt;everything&lt;/strong&gt;, Tomcat included. The server only needs Java 17 or newer (Spring Boot 3 needs at least Java 17).&lt;/li&gt;
&lt;li&gt;Stop a running app with &lt;code&gt;Ctrl + C&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The 2 startup errors everyone hits
&lt;/h2&gt;

&lt;p&gt;Both appear in a big box that says &lt;code&gt;APPLICATION FAILED TO START&lt;/code&gt;. Don't panic. Spring is very good at telling you exactly what's wrong. Read the &lt;strong&gt;Description&lt;/strong&gt; line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error 1: "Port 8080 was already in use"
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Description:
Web server failed to start. Port 8080 was already in use.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only one program can listen on a port at a time. Very often, the "other program" is &lt;strong&gt;your own app from an earlier run&lt;/strong&gt;, still alive in another terminal or IDE tab.&lt;/p&gt;

&lt;p&gt;Two fixes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stop the old one.&lt;/strong&gt; Find it and end it:

&lt;ul&gt;
&lt;li&gt;macOS/Linux: &lt;code&gt;lsof -i :8080&lt;/code&gt;, then &lt;code&gt;kill &amp;lt;PID&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Windows: &lt;code&gt;netstat -ano | findstr :8080&lt;/code&gt;, then &lt;code&gt;taskkill /PID &amp;lt;PID&amp;gt; /F&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use another port.&lt;/strong&gt; Add &lt;code&gt;server.port=8081&lt;/code&gt; to &lt;code&gt;src/main/resources/application.properties&lt;/code&gt;, or for one run: &lt;code&gt;java -jar target/*.jar --server.port=8081&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Error 2: "required a bean ... that could not be found"
&lt;/h3&gt;

&lt;p&gt;Say you add a new &lt;code&gt;EmailService&lt;/code&gt; and ask for it in the constructor of a &lt;code&gt;SignupController&lt;/code&gt;. You press Run and get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Description:
Parameter 0 of constructor in com.example.demo.SignupController required
a bean of type 'com.example.mail.EmailService' that could not be found.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may also see the cousin of this message: &lt;code&gt;No qualifying bean of type ...&lt;/code&gt;. Same meaning: "you asked for this object in a constructor, but it's not in my box."&lt;/p&gt;

&lt;p&gt;Think back to Step 2 and Step 3. There are two usual reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The class is outside the main package.&lt;/strong&gt; Here, &lt;code&gt;EmailService&lt;/code&gt; lives in &lt;code&gt;com.example.mail&lt;/code&gt;, a sibling of &lt;code&gt;com.example.demo&lt;/code&gt;. Component scan never walked in there. Fix: move it under &lt;code&gt;com.example.demo&lt;/code&gt; (for example &lt;code&gt;com.example.demo.mail&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You forgot the annotation.&lt;/strong&gt; A class without &lt;code&gt;@Service&lt;/code&gt; or &lt;code&gt;@Component&lt;/code&gt; is just a class. Spring won't create it. Fix: add the annotation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you know the startup story, this error reads like a sentence: "I scanned, I didn't find it, so I couldn't build the controller."&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'd learn next
&lt;/h2&gt;

&lt;p&gt;You now know what happens between clicking Run and seeing "Started". A real backend needs a lot more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;How a Spring Boot app starts, and what a bean is&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔒 Spring Core Fundamentals: bean scopes, &lt;code&gt;@Configuration&lt;/code&gt;, properties and profiles&lt;/li&gt;
&lt;li&gt;🔒 Building REST APIs: path variables, request bodies, status codes&lt;/li&gt;
&lt;li&gt;🔒 Layered Architecture: controllers, services and repositories&lt;/li&gt;
&lt;li&gt;🔒 Database and Spring Data JPA&lt;/li&gt;
&lt;li&gt;🔒 Validation and Exception Handling&lt;/li&gt;
&lt;li&gt;🔒 Spring Security&lt;/li&gt;
&lt;li&gt;🔒 Testing Spring Boot Applications&lt;/li&gt;
&lt;li&gt;🔒 API Documentation and Developer Tools&lt;/li&gt;
&lt;li&gt;🔒 Docker and Deployment&lt;/li&gt;
&lt;li&gt;🔒 Microservices Fundamentals&lt;/li&gt;
&lt;li&gt;🔒 Real-World Spring Boot Projects you can put on your CV&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Go from zero to hero with Spring Boot
&lt;/h3&gt;

&lt;p&gt;This post covered &lt;strong&gt;step 1 of 13&lt;/strong&gt;. The full &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=spring-boot-startup-3-seconds" rel="noopener noreferrer"&gt;Learn Spring Boot from Basic to Advanced&lt;/a&gt;&lt;/strong&gt; course on DevTrail takes you through all of it: &lt;strong&gt;13 modules and 86 hands-on lessons&lt;/strong&gt;, ending with real projects.&lt;/p&gt;

&lt;p&gt;Every lesson has exercises and quizzes you check &lt;strong&gt;right in your browser&lt;/strong&gt;, so you learn by doing, not by watching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module 1 is free.&lt;/strong&gt; &lt;a href="https://www.devtrail.info/courses/spring-boot/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=spring-boot-startup-3-seconds" rel="noopener noreferrer"&gt;Start the Spring Boot course →&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What Happens When You Call a FastAPI Endpoint</title>
      <dc:creator>Ravindra Singh</dc:creator>
      <pubDate>Thu, 03 Dec 2026 11:11:20 +0000</pubDate>
      <link>https://dev.to/ravindrame/what-happens-when-you-call-a-fastapi-endpoint-481p</link>
      <guid>https://dev.to/ravindrame/what-happens-when-you-call-a-fastapi-endpoint-481p</guid>
      <description>&lt;p&gt;You write eleven lines of Python. You run one command. And suddenly you have a working API, input checking, clear error messages &lt;strong&gt;and&lt;/strong&gt; a documentation page you never wrote.&lt;/p&gt;

&lt;p&gt;That's FastAPI. &lt;strong&gt;FastAPI is a Python framework for building APIs, where your type hints do the boring work: reading the request, checking the data, and writing the docs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But "it just works" is not a great feeling when something breaks. So let's follow one request, stop by stop, from the moment you press Enter to the moment JSON comes back.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 11 lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BaseModel&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/books"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"saved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install &lt;span class="s2"&gt;"fastapi[standard]"&lt;/span&gt;
fastapi dev main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open &lt;code&gt;http://127.0.0.1:8000/docs&lt;/code&gt;. There's a full, clickable docs page for your API. You didn't write it. Keep that in mind, we'll see where it comes from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow one request
&lt;/h2&gt;

&lt;p&gt;Someone sends this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;books&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Dune"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"412"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice &lt;code&gt;"412"&lt;/code&gt; is a &lt;strong&gt;string&lt;/strong&gt;, not a number. Watch what happens to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyqlomihrd11mcws8f68o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyqlomihrd11mcws8f68o.gif" alt="The request journey: Browser, Uvicorn, routing, Pydantic, your function, JSON" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Stop 1: Uvicorn, the doorman
&lt;/h3&gt;

&lt;p&gt;Your code doesn't listen on a port. &lt;strong&gt;Uvicorn&lt;/strong&gt; does. It's a server program that sits on port 8000, receives raw bytes from the network, and turns them into a neat Python dictionary describing the request (method, path, headers). That format is called &lt;strong&gt;ASGI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of Uvicorn as the doorman. It doesn't care what's inside the parcel. It just accepts it and passes it in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stop 2: Routing, the sorting desk
&lt;/h3&gt;

&lt;p&gt;Inside FastAPI, a router looks at two things: the &lt;strong&gt;method&lt;/strong&gt; (&lt;code&gt;POST&lt;/code&gt;) and the &lt;strong&gt;path&lt;/strong&gt; (&lt;code&gt;/books&lt;/code&gt;). It finds the one function that matches, &lt;code&gt;add_book&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;No match? You get a &lt;code&gt;404&lt;/code&gt; (wrong path) or &lt;code&gt;405&lt;/code&gt; (right path, wrong method), and your code never runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stop 3: Pydantic, the security check
&lt;/h3&gt;

&lt;p&gt;This is the clever part. FastAPI looks at your function signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It sees &lt;code&gt;book: Book&lt;/code&gt;, so it knows the body must look like a &lt;code&gt;Book&lt;/code&gt;. It hands the JSON to &lt;strong&gt;Pydantic&lt;/strong&gt;, which checks every field:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt; → &lt;code&gt;"Dune"&lt;/code&gt; is a string. ✅&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pages&lt;/code&gt; → &lt;code&gt;"412"&lt;/code&gt; is a string, but it &lt;em&gt;looks&lt;/em&gt; like a number, so Pydantic &lt;strong&gt;converts&lt;/strong&gt; it to &lt;code&gt;412&lt;/code&gt;. ✅&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now send something broken:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Dune"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"lots"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pydantic stops the request right here. Your function &lt;strong&gt;never runs&lt;/strong&gt;. The client gets a &lt;code&gt;422&lt;/code&gt; with a precise reason:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"int_parsing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"loc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"msg"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Input should be a valid integer, unable to parse string as an integer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"input"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"lots"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at &lt;code&gt;loc&lt;/code&gt;: it tells the caller &lt;em&gt;exactly&lt;/em&gt; which field was wrong. You wrote zero &lt;code&gt;if&lt;/code&gt; statements for this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 This is lesson one of the &lt;a href="https://www.devtrail.info/courses/fastapi/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=fastapi-request-journey" rel="noopener noreferrer"&gt;FastAPI course on DevTrail&lt;/a&gt;. You can break requests like this yourself and watch the 422s, right in your browser, with nothing to install.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Stop 4: Your function, finally
&lt;/h3&gt;

&lt;p&gt;By the time &lt;code&gt;add_book&lt;/code&gt; runs, &lt;code&gt;book&lt;/code&gt; is a real Python object. &lt;code&gt;book.pages&lt;/code&gt; is the integer &lt;code&gt;412&lt;/code&gt;, not a string. You can trust your data, so your function stays short and only does the actual work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stop 5: Back out as JSON
&lt;/h3&gt;

&lt;p&gt;You return a plain &lt;code&gt;dict&lt;/code&gt;. FastAPI turns it into JSON, adds the right headers and a &lt;code&gt;200&lt;/code&gt; status, and Uvicorn sends the bytes back to the client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The whole trip in one line:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bytes → Uvicorn → ASGI dict → router → Pydantic check → your function → JSON → bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  So where did the docs come from?
&lt;/h2&gt;

&lt;p&gt;Same place as the validation: &lt;strong&gt;your type hints.&lt;/strong&gt; FastAPI already knows the path, the method, and the exact shape of &lt;code&gt;Book&lt;/code&gt;. It writes that down in a standard format called &lt;strong&gt;OpenAPI&lt;/strong&gt;, and &lt;code&gt;/docs&lt;/code&gt; simply draws a page from it.&lt;/p&gt;

&lt;p&gt;One source of truth (your types) gives you three things: conversion, validation and documentation. That's the whole idea behind FastAPI.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake almost everyone makes: &lt;code&gt;async&lt;/code&gt; everywhere
&lt;/h2&gt;

&lt;p&gt;FastAPI supports &lt;code&gt;async def&lt;/code&gt;, so it's tempting to write it everywhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="nd"&gt;@app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/slow"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 🚨 blocks the whole server&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"done"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;async&lt;/code&gt; functions all share &lt;strong&gt;one&lt;/strong&gt; event loop. &lt;code&gt;time.sleep&lt;/code&gt; (or &lt;code&gt;requests.get&lt;/code&gt;, or a normal database driver) freezes that loop, so &lt;em&gt;every other request waits too&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The simple rule:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Your code calls...&lt;/th&gt;
&lt;th&gt;Write&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal blocking libraries (&lt;code&gt;requests&lt;/code&gt;, &lt;code&gt;time.sleep&lt;/code&gt;, most DB drivers)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;def&lt;/code&gt; (FastAPI runs it in a thread pool for you)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Async libraries you &lt;code&gt;await&lt;/code&gt; (&lt;code&gt;httpx.AsyncClient&lt;/code&gt;, &lt;code&gt;asyncpg&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;async def&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Plain &lt;code&gt;def&lt;/code&gt; is not slower or "old fashioned" in FastAPI. It's often the safe choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  FastAPI, Flask or Django?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;FastAPI&lt;/th&gt;
&lt;th&gt;Flask&lt;/th&gt;
&lt;th&gt;Django&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Best for&lt;/td&gt;
&lt;td&gt;APIs, microservices, ML model serving&lt;/td&gt;
&lt;td&gt;Small apps, quick prototypes&lt;/td&gt;
&lt;td&gt;Full websites with admin, auth, ORM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Validation&lt;/td&gt;
&lt;td&gt;Built in (Pydantic)&lt;/td&gt;
&lt;td&gt;Add a library&lt;/td&gt;
&lt;td&gt;Forms / DRF serializers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto docs&lt;/td&gt;
&lt;td&gt;Yes, &lt;code&gt;/docs&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Add a library&lt;/td&gt;
&lt;td&gt;With DRF + extra package&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Async&lt;/td&gt;
&lt;td&gt;Native&lt;/td&gt;
&lt;td&gt;Partial&lt;/td&gt;
&lt;td&gt;Partial&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Learning curve&lt;/td&gt;
&lt;td&gt;Low if you know type hints&lt;/td&gt;
&lt;td&gt;Lowest&lt;/td&gt;
&lt;td&gt;Higher, but batteries included&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you're building an &lt;strong&gt;API&lt;/strong&gt; in Python today, FastAPI is a very safe default.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'd learn next
&lt;/h2&gt;

&lt;p&gt;You now understand one request, start to finish. A real backend needs a lot more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;How a request flows through FastAPI&lt;/strong&gt; &lt;em&gt;(this post)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔒 Path and query parameters, and how they're validated&lt;/li&gt;
&lt;li&gt;🔒 Request bodies with nested Pydantic models&lt;/li&gt;
&lt;li&gt;🔒 Response models and status codes (hiding passwords from responses)&lt;/li&gt;
&lt;li&gt;🔒 Dependency injection, the feature that makes FastAPI apps clean&lt;/li&gt;
&lt;li&gt;🔒 A real PostgreSQL database with SQLAlchemy&lt;/li&gt;
&lt;li&gt;🔒 Login with JWT, password hashing, protected routes&lt;/li&gt;
&lt;li&gt;🔒 Testing, background tasks, middleware&lt;/li&gt;
&lt;li&gt;🔒 Deploying with Docker&lt;/li&gt;
&lt;li&gt;🔒 Full projects you can put on your CV&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Go from zero to hero with FastAPI
&lt;/h3&gt;

&lt;p&gt;This post covered &lt;strong&gt;step 1 of 11&lt;/strong&gt;. The full &lt;strong&gt;&lt;a href="https://www.devtrail.info/courses/fastapi/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=fastapi-request-journey" rel="noopener noreferrer"&gt;Learn FastAPI from Basic to Advanced&lt;/a&gt;&lt;/strong&gt; course on DevTrail takes you through all of it: &lt;strong&gt;11 modules and 49 hands-on lessons&lt;/strong&gt;, ending with real projects.&lt;/p&gt;

&lt;p&gt;Every lesson has code you run and check &lt;strong&gt;right in your browser&lt;/strong&gt;. No setup, no "works on my machine".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module 1 is free.&lt;/strong&gt; &lt;a href="https://www.devtrail.info/courses/fastapi/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=fastapi-request-journey" rel="noopener noreferrer"&gt;Start the FastAPI course →&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>api</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
