-
Notifications
You must be signed in to change notification settings - Fork 961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add location.index #334
Comments
On v3, the initial page load doesn't have a key at all. More broadly, if you left the SPA and came back, you should expect a new key – not |
You could do `${sessionNonce}:${index}` or just `${nonce}:${index}` though. That would give you the best of both worlds. |
Ya, this would fix that because you can just default to 0 now.
I like that idea. We could leave it up to people to generate a nonce for themselves to do things like restoring scroll position. If we provide the right primitives, people are empowered to do stuff like this. |
In fact, maybe we just introduce |
You need the nonce for correct state semantics even without transient stuff like scroll. Suppose:
For (3), history internally needs new keys; if not, at best you're going to clobber the location state associated with the locations in (1). |
Ah, yes. That makes sense. Thanks for helping me think through this. |
This would be an awesome addition, it would allow us to:
|
How about |
No – |
Hey guys, any thoughts on a timescale for this one? I'm still trying to implement proper back / forward buttons and somewhat blocked by this. Is the final conclusion to add the integer after the nonce as the location key? |
Turns out we can't actually do this in browsers without crazy, crazy hacks, which I'm not willing to introduce. We'll expose |
I was able to achieve this by keeping track of componentWillReceiveProps(nextProps: Props) {
const { location } = this.props;
const hasRouteChanged = nextProps.location !== location;
if (hasRouteChanged) {
switch (nextProps.location.action) {
case 'PUSH': {
this.historySinceEnter.push(nextProps.location.key);
break;
}
case 'POP': {
// Pop state changes fire for all navigations (back and forwards). We use the saved
// history state to distinguish between backward and forward navigations.
const lastHistoryIndex = this.historySinceEnter.length - 1;
const previousLocationKey = this.historySinceEnter[lastHistoryIndex - 1];
const isBack = previousLocationKey === nextProps.location.key;
if (isBack) {
this.historySinceEnter.pop();
} else {
this.historySinceEnter.push(nextProps.location.key);
}
break;
}
}
}
} I could be missing some edge cases though. Thoughts? |
@OliverJAsh How do you get access to |
I'm using React Router 2.1, and whatever version of History is used by that. |
Ah, that makes sense. Thanks for the info! |
The whole idea behind
location.key
was to provide a key that could be used to uniquely identify a page's position in the history stack. Couldn't we just use a numeric index into that stack?This means that on the initial page load
location.key
would be0
, which would potentially cause bugs for people doingif (location.key)
, but that's the only breaking change I can think of. Meanwhile, I think it may help us unlock a lot more functionality, especially around doing stuff like #36 which I've been thinking about for a while now...The text was updated successfully, but these errors were encountered: