Home > Back-end >  Rendered fewer hooks than expected. This may be caused by an accidental early return statement
Rendered fewer hooks than expected. This may be caused by an accidental early return statement

Time:09-22

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

CodePudding user response:

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.

  • Related