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

169
Visualizações
Keep input file text on re-creation of component

I have a functional component that will sometimes return a form for file selection, and sometimes it will return something else.

If I select my file, the name of the file will show up next to the "select file" button, however if I render something else, and then return to rendering the file input, the name of the file is lost, and the text goes back to "Select file..."

I'm keeping the file in state as shown below, but I'm unable to put the filename back on the input, so that the user can see that a file has been selected.

I've tried an approach using Ref as you can see below, but it's not working.

What can I do?

import { useState, useRef } from 'react'
import { Form } from 'react-bootstrap'

const MyComponent = () => {
    const [file, setFile] = useState(undefined)
    const fileRef = useRef(null)
    fileRef.current = file

    const onChange = event => {
        setFile(event.target.files[0])
    }

    if(something) {
        return (<div></div>)
    } else {
        return (<div>
            <Form.Group controlId="myFileInput" className="mb-3">
                    <Form.Label>Select file</Form.Label>
                    <Form.Control type="file" onChange={onChange} ref={fileRef}/>
            </Form.Group>
        </div>
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can fix it by using CSS display none instead remove this element

<div style={{display: something ? "block" : "none"}}></div>

<div style={{ display: !something ? "block" : "none" }}>
  <Form.Group controlId="myFileInput" className="mb-3">
    <Form.Label>Select file</Form.Label>
    <Form.Control type="file" onChange={onChange} ref={fileRef} />
  </Form.Group>
</div>
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