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

246
Visualizações
Why my InputBase focus doesn't work onClick React and Material UI

I'm facing strange bug. I'm trying to focus my input element when i click on button but that doesn't focus my input.

I've tried to use react hook useRef and then use it in my inputRef.

const inputRef = useRef<HTMLInputElement>(null);

        <InputBase
          inputRef={inputRef}
          value={search}
          placeholder="Search"
          onChange={onSearch}
          onKeyPress={onSearchQuery}
        />

      <Button
        variant="text"
        onClick={() => {
          inputRef.current?.focus();
          onOpen();
        }}
      >
        Focus my input
      </Button>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use React.createRef()

class MyClass extends React.Component {
        constructor(props) {
          super(props);
          // create a ref to store the textInput DOM element
          this.textInput = React.createRef();
          this.focus = this.focus.bind(this);
        }
      
        focus() {
            
          // Explicitly focus the text input using the raw DOM API
          // Note: we're accessing "current" to get the DOM node
          this.textInput.current.focus();
        }
      
        render() {
          // tell React that we want to associate the <input> ref
          // with the `textInput` that we created in the constructor
          return (
            <div>
    
            <InputBase
                ref={this.textInput} 
                value={search}
                placeholder="Search"
                onChange={onSearch}
                onKeyPress={onSearchQuery}
                />
    
                
    
                
            <Button variant="text" onClick={this.focus} >
                Focus my input
            </Button>
    
            </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