admin管理员组文章数量:1122832
I am trying to implement the basic modal using the parallel & intercepting route concept.
When I am in the intercepted route, say "/photo", I want the option to close the modal. So I have attached a close button on the modal. This is the modal component:
const PhotoModal = ({ children }: Props) => {
const dialogRef = useRef<ElementRef<"dialog">>(null);
const { push,back } = useRouter();
function onDismiss() {
// close modal
dialogRef.current?.close();
// and go back:
// back();
push("/");
}
if (typeof window == "object") {
return createPortal(
<dialog ref={dialogRef} onClose={onDismiss} open>
<button onClick={onDismiss} className="close-modal-btn">
X
</button>
{children}
</dialog>,
document.getElementById("root-modal")!
);
}
return null;
};
On clicking the close button, I want to navigate back to root route ("/"). I have tried:
back()
-> it takes me to the main "/photo" route.push("/")
-> it does takes me to "/" but when I click the link button which takes me to "photo" route nothing happens, it stays in the "/" route. So what I found out was that the intercepted route's page is still there. You can see this effect by muting thedialogRef.current?.close();
line. So my question is how do I go to "/" route?
I am trying to implement the basic modal using the parallel & intercepting route concept.
When I am in the intercepted route, say "/photo", I want the option to close the modal. So I have attached a close button on the modal. This is the modal component:
const PhotoModal = ({ children }: Props) => {
const dialogRef = useRef<ElementRef<"dialog">>(null);
const { push,back } = useRouter();
function onDismiss() {
// close modal
dialogRef.current?.close();
// and go back:
// back();
push("/");
}
if (typeof window == "object") {
return createPortal(
<dialog ref={dialogRef} onClose={onDismiss} open>
<button onClick={onDismiss} className="close-modal-btn">
X
</button>
{children}
</dialog>,
document.getElementById("root-modal")!
);
}
return null;
};
On clicking the close button, I want to navigate back to root route ("/"). I have tried:
back()
-> it takes me to the main "/photo" route.push("/")
-> it does takes me to "/" but when I click the link button which takes me to "photo" route nothing happens, it stays in the "/" route. So what I found out was that the intercepted route's page is still there. You can see this effect by muting thedialogRef.current?.close();
line. So my question is how do I go to "/" route?
2 Answers
Reset to default 0It seems like an issue with the Next.js, you can fix this by checking pathname using useEffect:
export default function ModalProvider() {
const pathname = usePathname();
const [open, setOpen] = useState(false);
useEffect(() => {
if (pathname === "/login") {
setOpen(true);
}
return () => setOpen(false);
}, [pathname]);
const router = useRouter();
return (
<Dialog
open={open}
onOpenChange={(open) => {
if (!open) router.back();
}}
>{...}</Dialog>
);
}
made this work by creating a anchor tag over the close button.
if (typeof window == "object") {
return createPortal(
<dialog ref={dialogRef} onClose={onDismiss} open>
<a href="/">
<button onClick={onDismiss} className="close-modal-btn">
X
</button>
</a>
{children}
</dialog>,
document.getElementById("root-modal")!
);
}
Theres no need of using useRouter in the close button click handler:
function onDismiss() {
// close modal
dialogRef.current?.close();
}
本文标签: nextjshow to navigate from intercepted route in nextjsStack Overflow
版权声明:本文标题:next.js - how to navigate from intercepted route in nextjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310267a1934317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论