[英]How to redirect after or during the onClick function of Button?
I am trying to add a feature in my Next.js website that allows users to click a button to create a new group, then redirect them to an "Invite members" page that uses the auto generated group_id in the URL with dynamic routing.我正在尝试在我的 Next.js 网站中添加一项功能,该功能允许用户单击按钮创建新组,然后将他们重定向到“邀请成员”页面,该页面使用 URL 中自动生成的 group_id 和动态路由。 I am currently using Next.js's Router, but I feel like there is a better (or working) way to do this.
我目前正在使用 Next.js 的路由器,但我觉得有更好(或工作)的方式来做到这一点。 JS (within
export default function groups ()
): JS(内
export default function groups ()
):
const [num, setNum] = useState("");
const router = useRouter()
const createGroup = async () => {
const { data, error } = await supabase
.from("chatgroups")
.insert([
{
group_name: groupName,
creator: user.email,
description: groupDesc,
accepted_members: user.email + " ",
invited_members: ""
}
]);
if (error) {
console.log(error);
} else {
setNum("/groups/" + String(data[0].group_id));
router.push(num);
}
};
HTML (returned in same JS script): HTML(在相同的 JS 脚本中返回):
<button type="button" className="bg-yellow-500 rounded px-12 py-2" onClick={()=> {
createGroup()
}} >Insert test group</button>
I have tried using the Router for both server and client and both did not recognize.push()我已经尝试将路由器用于服务器和客户端,但两者都没有识别.push()
import { Router } from "next/dist/client/router";
import { Router } from "next/dist/server/router"; //neither of these recognized the method.
My goal is to execute the function createGroup(), in which the value of "data" will receive the auto-generated int id of the new SQL row.我的目标是执行 function createGroup(),其中“data”的值将接收自动生成的新 SQL 行的 int id。 Then I want to use this id to redirect to "/groups/[id]", where [id] is the new ID.
然后我想用这个id重定向到“/groups/[id]”,其中[id]是新的ID。 I tried using a Link with the
const [num, setNum]
, but it performs the redirect BEFORE the new value is set.我尝试使用带有
const [num, setNum]
的链接,但它在设置新值之前执行重定向。 I am relatively new to Next.js, so I would appreciate any help with this.我对 Next.js 比较陌生,因此我将不胜感激。
Desired output: Click button -> adds row to SQL table with group_id = 17. Redirect user to "/groups/invite_members/17".所需的 output:单击按钮 -> 将行添加到 SQL 表,group_id = 17。将用户重定向到“/groups/invite_members/17”。
Edit: I have updated my main JS code above to use useRouter(), now it only works every second click.编辑:我已经更新了我上面的主要 JS 代码以使用 useRouter(),现在它只在每第二次点击时有效。
The issue is that calling setNum
does not update num
immediately , as setting state is an asynchronous operation.问题是调用
setNum
不会立即更新num
,因为设置 state 是异步操作。
This means that on the first button click num
will still have its default value ( ""
) when router.push(num)
is called, and only when clicking the button a second time will the num
state have updated with the value set previously.这意味着当
router.push(num)
时,在第一个按钮上单击num
仍将具有其默认值 ( ""
),并且只有当第二次单击该按钮时, num
state 才会更新为先前设置的值。
To fix it, you can set the value to a variable and use that in the router.push
call instead.要修复它,您可以将值设置为变量并在
router.push
调用中使用它。
const path = "/groups/" + String(data[0].group_id);
setNum(path);
router.push(path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.