How to properly integrate stacked drawers with react-router
#10563
Replies: 2 comments 2 replies
-
Hello @owenallenaz , Did you find a solution to this problem? I have a similar use case, I have to dismiss a modal when the user press the browser back button, I've been trying with |
Beta Was this translation helpful? Give feedback.
-
We ran into the same issue while building a similar stacked drawer system. Our solution was to track a navigation delta in the URL.
In our loaders and actions, we used an extended URL class with a set of increment/decrement functions to track it. The response from our actions return a Definitely, more complicated than it feels like it should be, given this could likely be implemented more easily in the client using something like We wanted to use actions and loaders to keep our data flow consistent with the rest of our app. class URLWithNavDelta extends URL {
incRootDelta() // manipulates this.searchParams
incNextDelta()
getRootDelta()
getNextDelta()
}
export async function loader({ request}) {
const url = new URLWithNavDelta(request.url)
return json({
rootDelta: url.getRootDelta()
})
}
export async function action({request}) {
const url = new URLWithNavDelta(request.url)
return json({
navDelta: url.getNextDelta()
})
}
function Route() {
const navigate = useNavigate()
const data = useLoaderData()
const onCancel = () => navigate(-data.rootDelta)
const fetcher = useFecter()
const actionData = fetcher.data
useEffect(() => {
if (fetcher.state !== 'idle') return
if (actionData?.navDelta > 0) navigate(-actionData?.navDelta)
}, [actionData, fetcher, navigate])
} To initialize this URL. // initializes the url with a root / next as 1 if the URL doesn't contain these values
<Link to={new URLWithNavDelta('path-to-open-drawer').toString()} /> |
Beta Was this translation helpful? Give feedback.
-
The application I'm working on has the ability to open drawers. What we would like to have happen is that when a user uses the native browser back button that it closes one level of the drawer system. We've tried a bunch of different ways to accomplish this task, but nearly all of them end up mired in complexity in hacks.
With 1 drawer open:
With 2 drawers open:
Some things we've tried:
<BrowserRouter>
in order to use our own event handler to intercept thepopstate
event that router router registers.The challenge is that each new drawer often has callbacks associated with the previous drawer. Due to the serializable nature of the
History.state
we can't store the callbacks in the state itself. This means we can't just the callbacks to the state object. There doesn't appear to be a clean way to differentiate between an action that triggers back via the UInavigate(-1)
which makes it difficult for us.Any advice on tackling this problem?
Beta Was this translation helpful? Give feedback.
All reactions