[英]How do I dynamically create route using a function return in next.js
I am trying to create a simple multiplayer game.我正在尝试创建一个简单的多人游戏。 I want to use a randomly generated room id in the route in order to route to the newly created lobby but can not pass the functions return to router.push as query ;
我想在路由中使用随机生成的房间 ID,以便路由到新创建的大厅,但不能将函数返回给 router.push 作为查询; I don't know why?
我不知道为什么? Can you help me?
你能帮助我吗?
const RandomRoom = () =>{
let romstr = "";
for(let i=0; i<5; i++){
const randomElement = randomchars[Math.floor(Math.random() * randomchars.length)];
romstr += randomElement;
}
return romstr;
}
const handleSubmit = function () {
return function () {
if (!executed) {
const param = RandomRoom;
executed = true;
socket.emit("joinroom", data1);
console.log("roomid" + param);
router.push({
pathname: `/lobby/[lobbyid]`,
query: {param}
});
}
}
}()
There's an issue with your router.push method, you do not provide lobbyid property to your query object.您的 router.push 方法存在问题,您没有为查询对象提供 lobbyid 属性。
const param = RandomRoom;
router.push({
pathname: '/lobby/[lobbyid]',
query: { lobbyid: param },
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.