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

189
Visualizações
Hoc React Hook "useEffect" cannot be called inside a callback

I am trying to use an HOC component in React with a functional component, but when using hooks I keep getting the error React Hook "useEffect" cannot be called inside a callback. What am I doing wrong and how can I solve this?

const HOC = (WrappedComponenet, entity) => {
  return function () {
    const [data, setData] = useState([]);
    const [term, setTerm] = useState("");

    useEffect(() => {
      fetch(`https://jsonplaceholder.typicode.com/${entity}`)
        .then((res) => res.json())
        .then((data) => setData(data));
    }, []);

    let filteredData = data.slice(0, 10).filter((d) => {
      if (entity === "users") {
        const { name } = d;
        return name.indexOf(term) >= 0;
      }
      if (entity === "todos") {
        const { title } = d;
        return title.indexOf(term) >= 0;
      }
    });

    return <WrappedComponenet data={filteredData} />;
  };
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Check out this snippet:

const HOC = (WrappedComponenet, entity) => {
  function MyCustomComponent() {
    const [data, setData] = useState([]);
    const [term, setTerm] = useState("");

    useEffect(() => {
      fetch(`https://jsonplaceholder.typicode.com/${entity}`)
        .then((res) => res.json())
        .then((data) => setData(data));
    }, []);

    let filteredData = data.slice(0, 10).filter((d) => {
      if (entity === "users") {
        const { name } = d;
        return name.indexOf(term) >= 0;
      }
      if (entity === "todos") {
        const { title } = d;
        return title.indexOf(term) >= 0;
      }
    });

    return <WrappedComponenet data={filteredData} />;
  };

  return <MyCustomComponent/>
};
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