Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

319
Vistas
Autofocus Input component Antd

I'm try to do autofocus Input component which is situated on Drawer component. All componen i take from Antd. The problem is that the autofocus does not work immediately but only after the window opens and closes. Link on CodeSandbox https://codesandbox.io/s/quizzical-morning-1nhi2v?file=/src/App.js

import './App.css';
import 'antd/dist/antd.css';
import {Button, Drawer, Input} from 'antd'
import React, { useState, createContext, useRef, useEffect } from 'react';

function App() {

  const  inputRef = useRef(true);
  const [visible, setVisible] = useState(false);

  const onClose = () =>{ //Close Drawer 
    setVisible(false);
  } 
  const open = () =>{ //Open Drawer and must be autofocus on Input
    setVisible(true);
  }
  useEffect(()=>{
    console.log(visible + 'useEffect');
    if(visible){
      inputRef.current.focus();
    }
  }, [visible])

  return (
    <div className="App">
      <Drawer visible={visible} onClose={onClose} autoFocus={true}>
        <Input ref={inputRef} ></Input>     
      </Drawer>     
      <Input ref={inputRef_}  ></Input>
      <Button onClick={open}>  Open</Button>
    </div>
  );
}

export default App; 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try this. I tested and it worked.

if (visible) {
  setTimeout(() => {
    inputRef.current.focus();
  }, [500]);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda