Blog
    By Gabriel CA
    5 min read

    What are the best practices for using React with TypeScript in 2026?

    What are the best practices for using React with TypeScript in 2026?

    React component props should have an object type, defined through a type alias or interface.1 React 19 removes function-component defaultProps and silently ignores propTypes checks.2

    For a React 19 project, start with explicit props and state contracts. Then review legacy defaults, ref handling, and the compiler setup using the guidance below.

    How should you define a component's props contract?

    The props object type describes the inputs your React component expects. React's guidance allows either a type alias or an interface for that description.1

    Use the hypothetical button below as a starting point for your own props contract. Choose names that explain each input's role, rather than copying this example unchanged.

    interface SaveButtonProps {
      label: string;
      disabled: boolean;
    }
    
    function SaveButton({ label, disabled }: SaveButtonProps) {
      return <button disabled={disabled}>{label}</button>;
    }
    

    When you review this contract, focus first on which inputs the button needs. Keep the choice between type and interface separate from your design review. Prefer clear names for each input over a rule that picks one spelling.

    How should you type React 19 hooks and actions?

    useActionState accepts an action function, initial state, and an optional permalink. It returns the current state, a dispatch function, and a pending boolean.3

    The action's return type must match the type of the initial state. If TypeScript infers types that do not match, you may need to state the type explicitly.3

    For the save sketch below, choose one shared type for the initial and returned state. This hypothetical example shows the type contract, not a complete flow that saves data.

    import { useActionState } from 'react';
    
    type SaveState = { message: string };
    const initialState: SaveState = { message: '' };
    
    async function saveAction(
      previousState: SaveState
    ): Promise<SaveState> {
      return { message: previousState.message };
    }
    
    function SaveStatus() {
      const [state, , isPending] = useActionState(
        saveAction,
        initialState
      );
    
      return <p>{isPending ? 'Saving' : state.message}</p>;
    }
    

    useActionState allows side effects and runs action calls in sequence. By contrast, useReducer manages UI state with a reducer that must be pure.3

    useOptimistic provides optimistic UI state and a setter for updating that state. Its update logic can use setter functions or a separate reducer function.4

    For the toggle below, use boolean for the state and the next value. This is a hypothetical type sketch; plan the full user action on its own.

    const [optimistic, dispatch] = useOptimistic(
      currentValue,
      (current: boolean, next: boolean): boolean => next
    );
    

    The reducer form keeps the update logic separate from the setter call.4 For this toggle sketch, review the state type and the next value together.

    When use reads a promise, that promise must be cached. The same promise instance must be reused across rerenders.5

    For a hypothetical user lookup, plan a props contract with a Promise<User> input. As a separate review step, decide where to keep that same promise between renders.

    Which legacy patterns should you replace in React 19?

    React 19 ignores propTypes checks and recommends TypeScript or another type-checking solution. It removes defaultProps from function components in favor of ES6 default parameters.2

    For the label example below, put the default value in the function's inputs. Treat this as a hypothetical design, and choose the text your own view needs.

    type LabelProps = { text?: string };
    
    function Label({ text = 'Save' }: LabelProps) {
      return <span>{text}</span>;
    }
    

    React 19 needs the new JSX transform to support ref as a prop. React 19's upgrade guide states that ref is now a regular prop.2 For React 19-only components, prefer a direct prop contract when reviewing existing forwardRef wrappers.

    React 19 deprecates reading element.ref in favor of element.props.ref.2 Do not read or write ref.current during rendering. Use state instead when that information is needed to render the UI.6

    How do unions keep asynchronous UI states consistent?

    The user view below is a hypothetical example of a three-state model. Use it to plan which fields belong with each status in your own view.

    type User = { name: string };
    
    type UserState =
      | { status: 'loading' }
      | { status: 'error'; error: Error }
      | { status: 'success'; data: User };
    
    function UserView({ state }: { state: UserState }) {
      switch (state.status) {
        case 'loading':
          return <p>Loading</p>;
        case 'error':
          return <p>{state.error.message}</p>;
        case 'success':
          return <p>{state.data.name}</p>;
        default: {
          const exhaustive: never = state;
          throw new Error(`Unhandled state: ${exhaustive}`);
        }
      }
    }
    

    Assigning the state left over to never checks that all cases are handled.7 Keep this check in the default branch when you extend your view's state model.

    When a data request ends, decide which state should reach the view next. If you add a status, review each view that switches on this contract.

    noFallthroughCasesInSwitch reports nonempty switch cases that lack break, return, or throw.8 Keep this check alongside the union's exhaustiveness check when reviewing state transitions.

    Which compiler settings and dependencies should you review?

    React's guide installs @types/react and @types/react-dom with the --save-dev flag for development use. You need dom in lib; it is included by default if lib is not set.1

    Use React's command below to install the latest React type definitions.1 Then check the settings your project has before you replace its compiler setup.

    npm install --save-dev @types/react @types/react-dom
    

    The jsx compiler option must use a valid setting. React's guide says preserve should suffice for most applications.1

    In TypeScript strict mode, add types to the inputs of your useCallback function. React's guide explains that inference from the return value leaves the callback's full type unclear without typed parameters.1

    React Compiler handles memoization for values and functions automatically, according to React's guide. This reduces the need for manual useCallback calls in code that uses the compiler.1

    If your project uses React Compiler, review callbacks before adding more caching code. Check strict typing and JSX settings on their own, not as steps that install the compiler.

    Sources

    1. React (react.dev) › typescript
    2. React (react.dev) › react-19-upgrade-guide
    3. React (react.dev) › useActionState
    4. React (react.dev) › useOptimistic
    5. React (react.dev) › use
    6. React (react.dev) › referencing-values-with-refs
    7. Steve Kinney (stevekinney.com)
    8. typescriptlang.org