admin管理员组文章数量:1291335
After a post request to an external API, I would like to redirect back to the homepage. I do have some knowledge with React and this is my first time using Next.js. Here is the code:
export default function New({genres}) {
const createMovie = (values) => {
console.log(values);
axios.post(`${process.env.NEXT_PUBLIC_BASE_URL}/movies`, {
title: values.title,
description: values.description,
genres: values.genres,
release_date: values.release_date,
cover_url: values.cover_url
}).then(res => {
const router = useRouter();
router.push('/');
})
}
As you can see I used router.push() but I get this error:
Error: Invalid hook call. Hooks can only be called inside of the body of a function ponent. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
What is the most efficient way to redirect to other pages in Next.js after a function and/or requests?
After a post request to an external API, I would like to redirect back to the homepage. I do have some knowledge with React and this is my first time using Next.js. Here is the code:
export default function New({genres}) {
const createMovie = (values) => {
console.log(values);
axios.post(`${process.env.NEXT_PUBLIC_BASE_URL}/movies`, {
title: values.title,
description: values.description,
genres: values.genres,
release_date: values.release_date,
cover_url: values.cover_url
}).then(res => {
const router = useRouter();
router.push('/');
})
}
As you can see I used router.push() but I get this error:
Error: Invalid hook call. Hooks can only be called inside of the body of a function ponent. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
What is the most efficient way to redirect to other pages in Next.js after a function and/or requests?
Share Improve this question asked Feb 5, 2021 at 21:19 user15067897user15067897 3-
Why don't you call
router.push()
from within the ponent? – elMeroMero Commented Feb 5, 2021 at 21:24 - @elMeroMero Because the navigation should only occur after the post request is done. That's why I put it inside then() – user15067897 Commented Feb 5, 2021 at 21:27
-
yes but you are calling
createMovie
from within the ponent, right? If so, let createMovie only return true or success message as a promise and let the ponent take care of the.then(res => { const router = useRouter(); router.push('/'); })
part. – elMeroMero Commented Feb 5, 2021 at 21:30
2 Answers
Reset to default 8You need to move where you call useRouter(). You can keep router.push() where it is.
export default function New({genres}) {
const router = useRouter();
const createMovie = (values) => {...}
}
If you look at the Rules of Hooks, you can only call the hook, useRouter()
in this case, at the top level.
I also had my initialization of useRouter
in my function. I fixed the same bug by placing that line into my function ponent instead of my function and calling router.push(...)
in the function itself.
本文标签: javascriptHow to programmatically navigate to other page in NextjsStack Overflow
版权声明:本文标题:javascript - How to programmatically navigate to other page in Next.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741529049a2383643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论