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

168
Visualizações
Property 'value' does not exist on type 'never'. when use useRef hook in mui

I am using material UI to build a login and registration page, using useRef to return a TextFiled ref instance, and xxxRef.current.value to get the input value.

I can smoothly run my project and can get the value correctly,but the console always reminded me that:

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

Here is my code snippets:

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 Respostas
Responde à pergunta

0

useRef is generic if you use it with TypeScript, so you can define the referenced element type like const ref = useRef<Type>();

Looking into the type definitions for the inputRef property in MaterialUI it states:

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

So for a fix you can define your refs like:

const accountRef = useRef<any>();

But the ref is passed through the input field inside the component, better type would be:

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

0

Correct answer should be using correct Type (HTMLInputElement) when using 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 Relatório

0

Note that there are other Types which could be added to useRef.

Some other types maybe :

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

useRef is commonly used for using a reference of a HTML element but it could also be used for storing data as well.

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