React problem z "infinite loop render"/"custom async hook"

0

Więc coś powoduje, że hook wykonuje się cały czas, hook musi wykonać prostą operację asynchroniczną.
Z reacta jestem dosyć zielony, a wykonywanie operacji asynchronicznych w hookach już wgl wydaje mi się być mocno skomplikowane, pojęcia nie mam dlaczego się tak dzieje.

import { useEffect, useState } from "react"
import { DbHandler } from "../../../utilities/dbHandler"
import { DefaultLogger } from "../../../utilities/logger"

export type UpdateIsAllowedFunction = () => Promise<void>

export const useIsDbAllowed = (): [boolean | null, boolean, UpdateIsAllowedFunction] => {
    const [isAllowed, setIsAllowed] = useState(null)
    const [isChecking, setIsChecking] = useState(false)

    //async execution wrapper
    const updateIsAllowed = async () => {
        if(isChecking) {
            DefaultLogger.warn(`check operation is in progress...`)
            return
        }

        try {
            setIsChecking(true)
            setIsAllowed(await DbHandler.isAllowed())
        } finally {
            setIsChecking(false)
        }
    }

    //execute on mount
    useEffect(() => {
        updateIsAllowed()
    }, [])

    return [isAllowed, isChecking, updateIsAllowed]
}

export default useIsDbAllowed

Z góry dzięki

0

jak to wywołujesz?

0

Jednak błąd był gdzie indziej, szkoda trochę, że react nie komunikuje błędów w trochę bardziej czytelny sposób

1 użytkowników online, w tym zalogowanych: 0, gości: 1