Skip to main content

Posts

Showing posts with the label React 19

React 19 Migration: How to Fix TypeScript Errors and Replace forwardRef

  The removal of   forwardRef   is one of the most significant developer experience improvements in React 19. For years,   ref   was treated as a special, magic attribute that required a Higher-Order Component (HOC) wrapper to tunnel through to a child component. In React 19,  ref  is just a prop. However, upgrading isn't seamless. If you simply strip the  forwardRef  wrapper, TypeScript will likely throw errors such as  Property 'ref' does not exist on type 'Props'  or fail to infer the correct HTML element type. This guide details the root cause of these errors and provides the standard pattern for typing and implementing refs in React 19. The Root Cause: Why code breaks In React 18 and earlier,  ref  and  key  were not actual props. When JSX was compiled, React stripped these attributes from the props object before passing the object to your component function. To access the  ref , you had to use  Re...

React 19: Handling Errors and Resets in useActionState Without Crashing

The introduction of  useActionState  (previously  useFormState ) in React 19 marks a paradigm shift from client-side event handlers to integrated Actions. However, developers migrating from  onSubmit  handlers are encountering a critical friction point: exception handling. In traditional React, an uncaught Promise rejection inside an  onClick  or  onSubmit  handler simply logs an error to the console. The UI remains interactive. In React 19's  useActionState ,  an uncaught error acts as a render error.  It propagates up the stack until it hits the nearest Suspense boundary or Error Boundary, potentially unmounting your entire feature. Furthermore, because these actions often run on the server, a failed validation or database error that isn't handled correctly causes the form state to desynchronize, often resetting controlled inputs to their initial values and frustrating users. Here is the root cause analysis and a production-g...