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

58
Visualizações
Creating a custom Hook that includes useRef

In the following code I create a hook that uses useState inside it, the hook works perfectly. Then I create a second custom hook that inside uses a useRef to print an html element in the console, the latter doesn't work because I don't know how to refer to the element from the hook I'm creating. Here the code:

import "./styles.css";
import {useEffect, useRef, useState} from "react"

function useChangeText(){
  const [n,setN]= useState(0)
  const sum= ()=> setN(n + 1)
  return {number:n,sum}
}

function useGetHTML(n){
  const element= useRef(null)
  useEffect(()=>{
    console.log(element.current)
  },[n])
}

export default function App() {
const hookSum= useChangeText()
const element= useGetHTML(hookSum.number)

  return (
    <div ref={element} className="App">
      <h1>{hookSum.number}</h1>
      <button onClick={hookSum.sum}>click +1</button>
    </div>
  );
}
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

It is a matter of exporting the ref created from your useGetHTML hook so that it can be used by your consuming component, just like you did in the useChangeText hook:

function useGetHTML(n){
  const element = useRef(null);
  useEffect(()=>{
    console.log(element.current)
  },[n]);
  
  return element;
}

Then in your component, the element will no longer be undefined but will be the exported ref from your custom hook:

const element = useGetHTML(hookSum.number);
about 4 years ago · Santiago Gelvez Relatório

0

You don't need useRef nor useEffect for that.

You can pass a setter callback to ref= it will be called with your element once your component is mounted:

import "./styles.css";
import {useState} from "react"

function useChangeText(){
  const [n,setN]= useState(0)
  const sum= ()=> setN(n + 1)
  return {number:n,sum}
}

function useGetHTML(n){
  const [element, setElementRef] = useState(null)
  if (element) { // just use if here
    console.log({ element, n })
  }
  return setElementRef
}
export default function App() {
const hookSum= useChangeText()
const setElementRef= useGetHTML(hookSum.number)

  return (
    <div ref={setElementRef} className="App">
      <h1>{hookSum.number}</h1>
      <button onClick={hookSum.sum}>click +1</button>
    </div>
  );
}
about 4 years ago · Santiago Gelvez 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