Skip to content

[fix] FileInput throws errors in reactStrictMode #3230

@JoeOakhartNava

Description

@JoeOakhartNava

ReactUSWDS Version & USWDS Version:

"react": "^19.0.0",
"react-dom": "^19.0.0",
"@trussworks/react-uswds": "^10.0.2",
"@uswds/uswds": "^3.13.0",
"next": "^15.3.3",

Describe the bug

When reactStrictMode is enabled in NextJS, components are exercised by unloading and reloading (recurisvelyTraverseAndDoubleInvokeEffectsInDEV). The purpose of this behavior is to catch issues that will impact users on rapid navigation.

Enabling reactStrictMode while using a FileInput component causes the FileInput component to throw an error when a file is selected.

<FileInput id="importFile" name="importFile" />

Failed to execute 'readAsDataURL' on 'FileReader': The object is already busy reading Blobs.

This error traces back to a line in the FilePreview component:

fileReaderRef.current.readAsDataURL(file)

To Reproduce
Steps to reproduce the behavior:

  1. Create a NextJS project from a template.
  2. Enable reactStrictMode in next.config.js.
  3. Add a FileInput component.
  4. Run the application and select a file using the FileInput component.
  5. Observe the error.

Expected behavior

File can be selected without throwing an error in reactStrcitMode.

Additional context

While the easiest way to replicate this issue is using NextJS's reactStrictMode, this bug represents a race condition that could affect any React application using the FileInput component.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working like it's supposed to

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions