Next.js Discord

Discord Forum

Invalid hook call

Answered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
I was trying to call a function for delaying before redirecting to an another page but it returns an error
Invalid hook call. Hooks can only be called inside of the body of a function component


I'm really confused, here's my code

function DelayRedirect() {
    useEffect(() => {
        const delay = 5000
        const timeoutId = setTimeout(() => {
            router.push('/')
        }, delay);
        return clearTimeout(timeoutId)
    }, [])
}
Answered by joulev
async handleSubmit() {
await fetch(…);
await new Promise(resolve => setTimeout(resolve, 5000));
router.push(…)
}
View full answer

31 Replies

@Clown As it says, you cannot use useEffect inside a function. You have to use it inside a function **component**
Pacific sand lanceOP
but i just put it inside of a function
@joulev The code is fine, show more code
Pacific sand lanceOP
wait
Yeah but where are calling it
@joulev The code is fine, show more code
Pacific sand lanceOP
const router = useRouter()
function DelayRedirect() {
    useEffect(() => {
        const delay = 5000
        const timeoutId = setTimeout(() => {
            router.push('/')
        }, delay);
        return clearTimeout(timeoutId)
    }, [])
}

export default function Pendaftaran() {
...
    const handleSubmit = async (event) => {
        event.preventDefault()
        setSubmitting(true)
        try {
            const userbody = { 
                bunch of data 
            }
                
            await fetch('/api/submitdata', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(userbody)
            })

            setSubmitting(false)

        } catch (error) {
            console.error(error)
            setSubmitfail(true)
            setSubmitted(false)
            }
        setSubmitted(true)
        DelayRedirect()
    }

return (
...
)
}
Pacific sand lanceOP
ah sorry
<DelayRedirect />
Pacific sand lanceOP
wait lemme try
If ts/jsx complains then add return null to that component
But what are you trying to do here? Redirect 5 sec after fetching or after page load?
Pacific sand lanceOP
after fetching
If you want to delay 5 sec after fetching, add the setTimeout inside the handleSubmit directly
Pacific sand lanceOP
soo what's the best way to do that
The useEffect is for delaying navigation after page load
Pacific sand lanceOP
i should put delay as a prop?
@Pacific sand lance soo what's the best way to do that
async handleSubmit() {
await fetch(…);
await new Promise(resolve => setTimeout(resolve, 5000));
router.push(…)
}
Answer
@joulev async handleSubmit() { await fetch(…); await new Promise(resolve => setTimeout(resolve, 5000)); router.push(…) }
Pacific sand lanceOP
so i don't need to use it as a component? just use this then?
Pacific sand lanceOP
i'll give u on updates if it works
@joulev Yes
Pacific sand lanceOP
it works! thanks a lot!
English Foxhound
error 404 while api post request
Error adding data: AxiosError: Request failed with status code 404
how to resolve this
can you open your own post with necessary information (ie code) - also: https://www.adios-axios.com
@English Foxhound error 404 while api post request
guessing by 404 you maybe have the wrong url or endpoint?
English Foxhound
but my url is correct
as i said, open a new post so we can see what you might have done wrong
English Foxhound
ok