#any way to get the previous route when using solid router?

88 messages · Page 1 of 1 (latest)

thorny robin
#

I don't think there's a built-in way, but you can always send the location with data and check if there's userData on a particular user page

#

Or you can use context, but I'm not fond of that solution

#

You don't need to send it all the time

#

Just send it for that particular navigation

#

Then context is a better option, right

#

Why wouldn't it?

#

This is precisely why you should consider routeData

#

Only that way you can be sure if the user came from /users - history.previous will not work most of the time

#

just add the data property to the route

#

and use it with useRouteData

#

no, you set the previous history entry there

#

You can set any type of data that you need for the route

#

You can also use state

#

If you just want to have the url in it

#

You would use useNavigate to go to the users page, and set the state there

#

and that state is in your location

#

Well, I don't think there's an easy solution to that

#

You can try to monkey patch stuff, but it's not a solution

#

And it surely is not clean

#

You have 1000 routes? Really?

#

And you always need state?

#

I.E. you need to know what's the previous location?

#

Or is it just in a particular place?

#

If that's the only locatin

#

location

#

Why don't you use useNavigate there, and set the state?

#

Hm, I don't think I understand completely what you're trying to say

#

Show me the code

#

And explain it a bit

#

And let's see

thorny robin
#

The browser API is not there because you should not be listening to user history for privacy reasons,

#

For SPA, you should use your router and pass the information along when you need it

#

You don't need to fork anything

#

But I see what could be a problem

#

I don't think you should control back or forward buttons at all, now that I look at the matter closer

#

If I came to the link from the email, and I want to go back, I would not be expecting that you stop me

#

It's not, if I didn't come from that page

#

At least IMHO

#

Hm, that sounds confusing

#

Why would that button need to know my history, if all it does is just brings me back to users page?

#

Why not just give it a route?

#

I think that solid's router remembers the position

#

Why is that relevant to a button which brings you back to /home/users?

#

If you want to go home, add a button for that

#

Well, I don't think that's a good UX

#

But you are free to do what you want

#

There's a back button in the browser

#

there's a back button in the browser toolbar, right?

#

You can have other navigation stuff

#

That's just my 2 cents

#

Why wouldn't the button just go back?

#

Did I came from that page?

#

Why would a back button transfer me back to a page I did not come from?

#

I am not

#

That makes sense

#

There's no where to go back

#

I don't think that should be happening with solid's router

#

If you come back with the back function

#

can't say for sure

#

So that back button works just as browsers back button

#

That doesn't sound like good UX to me

#

It's just how I feel

#

Not saying that you should not do it

#

If he is coming from an empty history, he needs to requery?

#

And you should have an explicit navigation for /home/users and /home and etc

#

useNavigate does not remember the scroll position?

#

Hm, I would not expect that

#

I would need to look at the site, since I don't have any projects with router

#

I know that solid's site uses it

#

I mean solidjs site

#

Sorry if I couldn't help more

still quartz
#

export function Link(props: LinkProps) {
const location = useLocation();

return <A activeClass={''} inactiveClass={''} state={{ previous: location.pathname }} {...props} />;
}

#

const location = useLocation();

const historyBack = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (location.state && (location.state as any).previous) {
navigate(-1);
} else {
navigate('/');
}
};

#

Oh,I see. maybe you can use a context save link history

#

solid-router has a useBeforeLeave hook.

#

save location in this hook

#

define a global list or context list. add location in useBeforeLeave.

#

next page get lastest list item is your prev router

#

oncleanup is component hook. not router

#

if solid-router has a after hook, it's very easy. userBeforeLeave has some limit

#

no.

#

oncleanup not trigger only route change

#

not only. not only not

#

some bowser will free page memory. mayby trigger oncleanup

hollow marten
#

Sorry, it's long thread I didn't read all the comments, but the solution I found to work is this

const onClickHandler = () => {
    const path = pathname()
    window.history.back()
    // If there are no entries inside history back button
    // won't work and user will be stuck.
    // Example: user loads new tab going straight to /settings,
    // when app back button is pressed, nothing happens because
    // this is the first entry in history.
    // To prevent this check if URL changed, after short delay,
    // if it didn't back button most likely didn't do anything.
    wait(50).then(() => {
      if (path === pathname()) {
        navigate('/')
      }
    })
  }