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

166
Visualizações
Rendered fewer hooks than expected. This may be caused by an accidental early return statement

I'm getting this error when triggering a setState inside of a custom React hook. I'm not sure of how to fix it, can anyone show me what I'm doing wrong. It is getting the error when it hits handleSetReportState() line. How should I be setting the report state from inside the hook?

custom useinterval poll hook

export function usePoll(callback: IntervalFunction, delay: number) {
const savedCallback = useRef<IntervalFunction | null>()

useEffect(() => {
    savedCallback.current = callback
}, [callback])

useEffect(() => {
    function tick() {
        if (savedCallback.current !== null) {
            savedCallback.current()
        }
    }
    const id = setInterval(tick, delay)
    return () => clearInterval(id)
}, [delay])
}

React FC

const BankLink: React.FC = ({ report: _report }) =>  {

const [report, setReport] = React.useState(_report)

if ([...Statues].includes(report.status)) {
    
    usePoll(async () => {
        const initialStatus = _report.status
        const { result } = await apiPost(`/links/search` });
        const currentReport = result.results.filter((item: { id: string; }) => item.id === _report.id)

        if (currentReport[0].status !== initialStatus) {
            handleSetReportState(currentReport[0])
            console.log('status changed')
        } else {
            console.log('status unchanged')
        }
    }, 5000)
}
... rest
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is because you put usePoll in if condition, see https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

You can put the condition into the callback

usePoll(async () => {
    if ([...Statues].includes(report.status)) {
        const initialStatus = _report.status
        const { result } = await apiPost(`/links/search` });
        const currentReport = result.results.filter((item: { id: string; }) => item.id === _report.id)

        if (currentReport[0].status !== initialStatus) {
            handleSetReportState(currentReport[0])
            console.log('status changed')
        } else {
            console.log('status unchanged')
        }
    }
}, 5000)

And if the delay will affect report.status, use ref to store report.status and read from ref value in the callback.

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