1

im have problem with upload file with inertia react and laravel 9 i use this exact code but when i choose a file i got errors in console https://inertiajs.com/file-uploads

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. More info: https://reactjs.org/link/controlled-components
input
form
Create@http://127.0.0.1:5173/resources/js/Pages/Admin/Setting/Content/ContactUs/Create.jsx?t=1659389601569:28:14
s@http://127.0.0.1:5173/node_modules/.vite/deps/@inertiajs_inertia-react.js?v=0d793d15:741:16

The above error occurred in the <input> component:

input
form
Create@http://127.0.0.1:5173/resources/js/Pages/Admin/Setting/Content/ContactUs/Create.jsx?t=1659389680993:28:14
s@http://127.0.0.1:5173/node_modules/.vite/deps/@inertiajs_inertia-react.js?v=0d793d15:741:16

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

this is my code

import React from "react";

import Button from "@mui/material/Button";
import { Link, useForm, usePage } from "@inertiajs/inertia-react";

export default function Create() {
    const { data, setData, post, progress } = useForm({
        name: null,
        avatar: null,
    });

    function submit(e) {
        e.preventDefault();
        post("/users");
    }

    return (
        <form onSubmit={submit}>
            <input
                type="text"
                value={data.name}
                onChange={(e) => setData("name", e.target.value)}
            />
            <input
                type="file"
                value={data.avatar}
                onChange={(e) => setData("avatar", e.target.files[0])}
            />
            {progress && (
                <progress value={progress.percentage} max="100">
                    {progress.percentage}%
                </progress>
            )}
            <button type="submit">Submit</button>
        </form>
    );
}

i think its problem with onChange={(e) => setData("avatar", e.target.files[0])}

1 Answer 1

0

I had the same issue... I solved removing the value attribute from the input type="file".

 <input
    type="file"
    onChange={(e) => setData("avatar", e.target.files[0])}
 />

(If you see in the documentation the same example for svelte or vue, you will not see the value attribute there).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.