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)
}
}