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

105
Visualizações
How to make Custom Hook returns a function, which uses another hook

So I have a custom hook, which I want to use at multiple places in another components like a function, which takes in parameter (E.i For useNavigate() from react-router-dom-v6, we use it as const navigate = useNavigate() then navigate('./url/to/navigate') can be used at multiple places).

But in the custom hook, I want to process that parameters value through another custom hook. This is what I tried but React does not allow me to have hooks inside function like so:

//useCustomHook.js
function usePrevious(value) {
    const ref = useRef();
    useEffect(() => {
        ref.current = value;
    });
    return ref.current;
}
...
export default function useCustomHook() {
    function processingData(currentState){
        const previousState = usePrevious(currentState);    //React does not allow to have hook here
        return process(currentState, previousState);
    }
    return processingData;
}

//Component.js
...
const cusHook = useCustomHook();
...
useEffect(()=>{
   cusHook(state1)
},[])

useEffect(()=>{
   cusHook(state2)
},[temp])

Is there a way I can achieve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No, there is no way you can use hook inside pure javascript function. Why not doing something like this:

   export default function useCustomHook(currentState) {
        const previousState = usePrevious(currentState);
        const processingData = () => {
               return process(currentState, previousState);
        }
       return processingData;
   }
about 4 years ago · Juan Pablo Isaza 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