Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Proper way to change Form.Control text with custom onChange

I noticed today that while using a custom onChange in a Form.Control that the text in the field no longer changes when a file is selected. I have looked through the documentation on the Git HERE but it doesnt say how to change the text with a custom event.

my code:

    // handles when a file has been selected. 
    const handleUploadChange = (event) =>
    {
        setFileToUpload(event.target.files[0]);
        setIsFilePicked(true);
    }

    // Footer containing buttons and file selection
    const CardFooter = () =>{
        return(
            <Card.Footer>
                <div style={{width:"80%", display : "flex", flexDirection: "row", justifyContent: "center"}}>
                    <div style={{marginRight: "40px"}}>
                        <Form.Group controlId="formFile" className="mb-3">
                            <Form.Control type="file" custom onChange={handleUploadChange} label={fileToUpload === null ? "No File Selected" : fileToUpload.name}/>
                        </Form.Group>
                    </div>
                    <div className="btn-red" style={{marginRight: "40px"}}>
                        <Button disabled={fileToUpload === null} onClick={()=>{setFileToUpload(null); setIsFilePicked(false);}}>Clear</Button>
                    </div>
                    <div>
                        <Button disabled={(fileToUpload === null) || (fileToUpload.size >= maxUploadSizeInBytes)} onClick={()=>handleSubmit(fileToUpload)}>Upload</Button>
                    </div>
                </div>
            </Card.Footer>
        )
    }

As you can see here, once a file is selected, my change event happens, but the selection box still reads "No file chosen". I have attempted label, text, displayName , and placeholder like so: label={fileToUpload === null ? "No File Selected" : fileToUpload.name}

enter image description here

Does anyone know the proper prop to use with a custom onChange ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't control input of type "file" in React. When you choose a file, the text/"label" should automatically be updated. You don't have to manage it. Just set the state on select and read the state on submission. To fix your clear button, you could use useRef:

const fileInputRef = useRef(null);
...
<Form.Control ref={fileInputRef} ...>
...
<Button onClick={() => {
    setFileToUpload(null);
    fileInputRef.current.value = null;
}}>
    Clear
</Button

Also consider following:

  • You don't need your "isFilePicked" state. It can be derived from "fileToUpload" with Boolean(fileToUpload).
  • There is no Form.Control custom property.
  • Nore there is a Form.Control label property. If you need a label, add Form.Label to it. You can hide the file input and style your own input solution if you want to.

Codesandbox:

https://codesandbox.io/s/beautiful-vaughan-zn68t

Read more:

how to hold file input value in react

How to reset ReactJS file input

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!