English 中文(简体)
防止浏览器返回纽特敦,进入MUI 模式/方log上页,进入新的下一个JS信号灯
原标题:Prevent browser back button from going to previous page for MUI modal/dialog in new NextJS approuter

我使用的是MUI、打字器、 next子和新的指路器。

在模式开放时,我就劫持了浏览器背后纽子的行为,这样它就能够关闭模式,而不是进入浏览器上前页,这是流动用户的直觉行为。

请使用这一简化碎块以避免复杂性:

"use client"
import { Modal } from  @mui/material 
import { useState } from  react 

export default function Component() {
    const [open, setOpen] = useState(false)

    return (
        <>
            <Modal
                open={open}
                onClose={() => setOpen(false)}
                style={{ display: "flex", alignItems: "center", justifyContent: "center" }}
            >
                <div style={{ background: "white", padding: "100px" }}>
                    hi
                </div>
            </Modal>
            
            <button onClick={() => setOpen(true)}>
                open modal
            </button>
        </>
    )
}
问题回答

你们需要背景路线

这里的例子有:

Source code: https://github.com/vercel/nextgram?tab=readme-ov-file

Demo app: https://nextgram.vercel.app/

官方出版物:

https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes

平行和拦截路线是它们特别为环境路线添加的新特征

You will need to

  • Create a modal route that intercepts your original route

enter image description here

  • Instead of opening your modal with state - navigate to the same route with Link on the client
// code inside of .../photo/id 

export default function YourPage() {
  return (
   <div>
     <nav>whatever</nav>
     <div>whatever</div>
     <Link href="/photo/id">
       open modal
     </Link>

   </div>
  )
}

and this will open the route on

.../@modal/photo/id

页: 1





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签