#async useEffect

8 messages · Page 1 of 1 (latest)

uneven crater
#

Hi! I'm trying to make an api query (async call) when certain states change so that my app re-renders with up-to-date data.

I followed the "use function outside" answer from stackoverflow: https://stackoverflow.com/questions/53332321/react-hook-warnings-for-async-function-in-useeffect-useeffect-function-must-ret and also browsed the github issue linked there.

As a result, my code works in development mode and does exactly what I want it to do now! But when I try to do a production build I get:
49:9 Error: React Hook "useAsyncEffect" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function. react-hooks/rules-of-hooks

Is there some way I can make tweak the production build settings to act more like development mode? Or is there a ~simple way to change my code (I don't really understand the useCallback option from the stackoverflow post.

Here's what a section of my code looks like:

    const [availableFeeds, setAvailableFeeds] = useState([])
    const [publishedChannels, setPublishedChannels] = useState([])
    const [publishedRoles, setPublishedRoles] = useState([])
    const [guildRoles, setGuildRoles] = useState([])
    const [guildChannels, setGuildChannels] = useState([])

    let abe_guild_data_db
    useEffect(() => {
        useAsyncEffect()
    }, [useAsyncEffect])//, [publishedChannels,publishedRoles])

    const useAsyncEffect = async() => {
        let response = await fetch("/api/abe-guilds-data?guild_id=" + String(guild_id), {
            method: "GET",
        })
        abe_guild_data_db = await response.json()

        setGuildChannels(abe_guild_data_db.channels)
        setGuildRoles(abe_guild_data_db.roles)

        let availableFeedsLocal = Array.from(Object.keys(abe_guild_data_db.feeds_to_channels))
        setAvailableFeeds(availableFeedsLocal)
        
        if (String(publishedChannels) === String([])) {
            let publishedChannelsLocal = []
            let publishedRolesLocal = []

            availableFeedsLocal.forEach(function(key){
                publishedChannelsLocal.push(abe_guild_data_db.feeds_to_channels[key])
                publishedRolesLocal.push(abe_guild_data_db.feeds_to_roles[key])
            });

            setPublishedChannels(publishedChannelsLocal)
            setPublishedRoles(publishedRolesLocal)
        }
    }
#

Ah and I'm using next:12.3.1 and react:18.2.0

stone aspen
#

useCallback is there so that the function is not recreated on every render. Without it, the value of the dependency of useEffect will be different every single render, causing it to trigger again.

#

Wrap it in a useCallback and put it above useEffect. Not sure if the order is relevant, but I think it probably is. At least I always write my code that way.

#

Then rename useAsyncEffect, because it's not an actual hook. This is most likely the error in production.

uneven crater
stone aspen
#

The "use" is the problem

#

Hooks start with "use". You can't use hooks within useEffect.