Skip to content

fix: update FilePreview to handle multiple renders in strict mode#3236

Merged
brandonlenz merged 3 commits intotrussworks:mainfrom
doug-s-nava:dws-file-input-dev-mode-2433
Nov 7, 2025
Merged

fix: update FilePreview to handle multiple renders in strict mode#3236
brandonlenz merged 3 commits intotrussworks:mainfrom
doug-s-nava:dws-file-input-dev-mode-2433

Conversation

@doug-s-nava
Copy link
Contributor

@doug-s-nava doug-s-nava commented Aug 13, 2025

Summary

A bug exists in the FilePreview component that prevents the FileInput component from correctly rendering when a file is uploaded in React Strict Mode. Because Strict Mode requires components to render twice, the mount logic in FilePreview will run twice, causing an error to be throw in relation to the FileReader object being accessed multiple times:

Error: An attempt was made to use an object that is not, or is no longer, usable

Note that this fix comes directly from the comment from @TomNUSDS (hi Tom!) here #2433 (comment)

Related Issues or PRs

Closes #2433

How To Test

Reproduce bug

  1. checkout the main branch
  2. add react strict mode to storybook configuration. In .storybook/main.ts, framework config should look like
  framework: {
    name: '@storybook/react-vite',
    options: {
      strictMode: true,
    },
  },
  1. start storybook with npm run storybook
  2. navigate to http://localhost:9009/?path=/docs/components-file-input--docs
  3. upload a file using the file input
  4. VERIFY: error message appears (see screenshot)

Verify fix

  1. check out this branch
  2. repeat steps 2 - 5 from above
  3. VERIFY: file uploads successfully, no error message

Screenshots (optional)

Screenshot 2025-08-13 at 8 34 53 AM

@doug-s-nava doug-s-nava marked this pull request as ready for review August 13, 2025 12:46
@doug-s-nava doug-s-nava requested a review from a team as a code owner August 13, 2025 12:46
@doug-s-nava doug-s-nava force-pushed the dws-file-input-dev-mode-2433 branch 2 times, most recently from 136382d to 3964ede Compare August 13, 2025 12:47
Copy link
Contributor

@brandonlenz brandonlenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix looks great to me. Thanks for PRing it. Thanks @TomNUSDS for suggesting it.

@brandonlenz brandonlenz merged commit 9ee684f into trussworks:main Nov 7, 2025
8 checks passed
@brandonlenz
Copy link
Contributor

@all-contributors please add @doug-s-nava for bug, code

@allcontributors
Copy link
Contributor

@brandonlenz

I've put up a pull request to add @doug-s-nava! 🎉

@brandonlenz
Copy link
Contributor

@all-contributors please add @TomNUSDS for bug, code

@allcontributors
Copy link
Contributor

@brandonlenz

I've put up a pull request to add @TomNUSDS! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[fix] FilePreview component breaks FileReader use in onChange events

2 participants