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

165
Views
La propiedad 'valor' no existe en el tipo 'nunca'. cuando use el gancho useRef en mui

Estoy usando material UI para crear una página de inicio de sesión y registro, usando useRef para devolver una instancia de referencia de TextFiled y xxxRef.current.value para obtener el valor de entrada.

Puedo ejecutar mi proyecto sin problemas y obtener el value correctamente, pero la consola siempre me recordó que:

 Property 'value' does not exist on type 'never'.

Aquí están mis fragmentos de código:

 const accountRef = useRef(); <TextField variant="outlined" margin="normal" required fullWidth id="account" label="Account" name="account" autoComplete="account" autoFocus defaultValue={account} inputRef={accountRef} /> const payload = { account: accountRef.current?.value ?? '', password: passwordRef.current?.value ?? '', nickname: nicknameRef.current?.value ?? '', };
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

useRef es genérico si lo usa con TypeScript, por lo que puede definir el tipo de elemento al que se hace referencia como const ref = useRef<Type>();

Mirando las definiciones de tipo para la propiedad inputRef en MaterialUI, dice:

 /** * Pass a ref to the `input` element. */ inputRef?: React.Ref<any>;

Entonces, para una solución, puede definir sus referencias como:

 const accountRef = useRef<any>();

Pero la referencia se pasa a través del campo de entrada dentro del componente, el mejor tipo sería:

 const accountRef = useRef<HTMLInputElement>();
over 4 years ago · Santiago Trujillo Report

0

La respuesta correcta debe ser el tipo correcto (HTMLInputElement) al usar useRef():

 const inputRef = useRef<HTMLInputElement>(); const [caretPosition, setCaretPosition] = useState<number | null>(null); const updateCaretPosition = () => { inputRef.current && setCaretPosition(inputRef.current.selectionStart); };
over 4 years ago · Santiago Trujillo Report

0

Tenga en cuenta que hay otros tipos que podrían agregarse a useRef.

Algunos otros tipos tal vez:

  • HTMLElement
  • HTMLFormElement
  • number
  • string
  • etc...

useRef se usa comúnmente para usar una referencia de un elemento HTML, pero también podría usarse para almacenar datos.

over 4 years ago · Santiago Trujillo 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!