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

206
Visualizações
How to make a generic callback function react

I am creating a callback function for passing setState up from child to parent. However, I have found myself creating tonnes of functions for each type of state im using and was wondering if there would be a way to make this generic. Here is an example of one of the functions:

    const setIsModalVisible = useCallback(val => {
    setModalVisible(val);
  }, [setModalVisible]);

Each function is the same but using a different setState. Any help would be great! Thanks

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

0

Create a function which returns callback

const GetCallback = (func) => {
    return useCallback(
        (val) => {
          func(val);
        },
        [func]
    );
};

and call that function by passing the setState as a argument.

CodeSandbox Link - https://codesandbox.io/s/quiet-lake-h2pzr?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Relatório

0

React hooks (so useCallback too) must be used in root of the component function. Therefore You cannot make some generator for creating multiple useCalback.

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