Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda