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

144
Visualizações
how to resolve cannot use Hooks inside useEffect

This is my function I am importing from another file

import { useMemo } from 'react'
import { useIntl } from 'react-intl'

export function useRenderStatus(param) {
  const { messages } = useIntl()
  const data = useMemo(() => {
    if (param === 'ACTIVE') return messages.page.active
    if (param === 'INACTIVE') return messages.page.inactive
    return 'invalid'
  }, [messages, param])

  return data
}

And while I query the data in my main file, I parse it into the needed language in this way..

  useEffect(() => {
    if (data) {
      const parsed = data.map((item) => ({
        ...item,
        status: useRenderStatus(item.status),
      }))
      setData(parsed)
    }
  }, [data, useRenderStatus])

Here I get the error that I cannot useRenderStatus inside useEffect, what would my alternative approach be?

I need the useRenderStatus to be in a separate file because I have many other similar functions like that which I want to reuse, all inside useEffect. How to solve this issue?

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

0

Since hooks must be executed in the same order, every time, you cannot use hooks inside useEffect, since that executes conditionally

https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

about 4 years ago · Juan Pablo Isaza Relatório

0

You'll need to refactor the guts of useRenderStatus into a free function and then call it:

import { useMemo } from "react";
import { useIntl } from "react-intl";

function getStatus(messages, param) {
  if (param === "ACTIVE") return messages.page.active;
  if (param === "INACTIVE") return messages.page.inactive;
  return "invalid";
}

// Not used in this example, but shows the refactoring
function useRenderStatus(param) {
  const { messages } = useIntl();
  return useMemo(() => getStatus(messages, param), [messages, param]);
}

function Component() {
  const { messages } = useIntl();
  useEffect(() => {
    if (data) {
      const parsed = data.map((item) => ({
        ...item,
        status: getStatus(messages, item.status),
      }));
      setData(parsed);
    }
  }, [messages, data]);
}

This will trigger an infinite render loop bug, however, since your effect depends on the state atom it itself sets.

It's better to use useMemo to derive the status-set state based on the data:

function Component() {
  const {messages} = useIntl();
  const data = [/* ... */];
  const dataWithStatuses = useMemo(() => {
    if (data) {
      return data.map((item) => ({
        ...item,
        status: getStatus(messages, item.status),
      }))
    }
    return null;
  }, [messages, data]);
}

Once you've done that, you could wrap that into a custom hook...

function useDataWithStatuses(data) {
  const {messages} = useIntl();
  return useMemo(() => {
    if (data) {
      return data.map((item) => ({
        ...item,
        status: getStatus(messages, item.status),
      }))
    }
    return null;
  }, [messages, data]);
}

function Component() {
  const data = [/* ... */];
  const dataWithStatuses = useDataWithStatuses(data);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

useRenderStatus returns data...you have it on the dependencies. What you are passing to useEffect is a callback that will be called when the dependencies change...useRenderStatus will not change unless the reference changes... Call useRenderStatus outside of the useEffect to get the value of data. Use data as the value of status maybe? useMemo is useful for memoizing the result of calling a function doing some calculation and not running the function on every render(only when the array of dependencies changes)

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