Remove value Prop from File Input in React File Upload Example #396
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR removes the
value={data.avatar}
attribute from the file input in the React file upload example (resources/js/Pages/file-uploads.jsx
). This change addresses two React errors (the application stops, the screen goes black and nothing is rendered when I select a file):Error 1:
"Warning: value prop on input should not be null. Consider using an empty string to clear the component or undefined for uncontrolled components."
File inputs are read-only for security reasons, and binding a value can lead to this warning.
Error 2:
"Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component."
Removing the value prop ensures that the file input remains uncontrolled, avoiding the warning.
Solution:
Simply remove
value={data.avatar}
from the file input. This aligns with best practices for handling file inputs in React and resolves the errors mentioned above.Note:
I have only tested these changes in an InertiaJS setup using React and Laravel as the backend. I haven't verified the examples for Vue or other frameworks. Please ensure that similar changes for other frontend frameworks are tested accordingly if needed.