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

139
Visualizações
How do i access a ref.current.value outside of the set scope

Here i have some React code ->

React refs

 const noteTitleRef = React.createRef();
 const noteBodyRef = React.createRef();

Button component

           <LoadingButton
                  color="secondary"
                  onClick={() => {
                    setLoading(true);
                      addNote(noteTitleRef.current.value, noteBodyRef.current.value);
                      setLoading(false);
                  }}
                  loading={loading}
                  loadingPosition="start"
                  startIcon={<SaveIcon />}
                  variant="contained"
                >
                  Save
                </LoadingButton>

If i change Button component to

           <LoadingButton
                  color="secondary"
                  onClick={() => {
                    setLoading(true);
                    setTimeout(() => {
                      addNote(noteTitleRef.current.value, noteBodyRef.current.value);
                      setLoading(false);
                    }, 1000);
                  }}
                  loading={loading}
                  loadingPosition="start"
                  startIcon={<SaveIcon />}
                  variant="contained"
                >
                  Save
                </LoadingButton>

Then the noteTitleRef.current.value & noteBodyRef.current.value becomes null and doesn't work.

How do i get around this? I would like to set a delay to my onClick, and after 1 second then i want the addNote() function to fire passing the noteTitleRef.current.value, noteBodyRef.current.value as parameters.

I appreciate all feedback!

about 4 years ago · Juan Pablo Isaza
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