Skip to content

fix: update state key references from 'key' to '__TSR_key' #4356

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

naoya7076
Copy link

@naoya7076 naoya7076 commented Jun 10, 2025

Description

Currently, when using TanStack Router, if users define a property named key within the navigation state, its value is overridden by the router's internal logic, which assigns a random key.

For example:

#284 (comment)

const navigate = useNavigate();
navigate({ to: '/', state: { color: 'blue', key: 123 } });

// Defined type
declare module '@tanstack/react-router' {
  interface HistoryState {
    key?: number
    otherValue?: string
  }
}

// On navigation target
const location = useLocation();
console.log(location.state.color); // correctly "blue"
console.log(location.state.key);   // incorrectly overridden by a random string like "p7axhg"

Expected Behavior

When explicitly providing a key within the navigation state, it should preserve the user's defined value rather than being overridden by the router's internal random key.

Copy link
Member

@SeanCassiere SeanCassiere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naoya7076 thanks for opening a PR for this, but we can't ship this in within the 1.0 release-cycle since this'd be considered a breaking change.
Since we have user-facing functions (like getScrollRestorationKey) which rely on location.state.key being defined/accessible.

I'll add this to the v2 list for it to be renamed to __TSR_key.

Edit: I've added this to the v2 list.

@naoya7076
Copy link
Author

@SeanCassiere Thank you again for your feedback. I've changed __key to __TSR_key.  If merging it for v2 is not too much trouble, I would be very happy to see it included.

@naoya7076 naoya7076 changed the title fix: update state key references from 'key' to '__key' fix: update state key references from 'key' to '__TSR_key' Jun 13, 2025
@SeanCassiere
Copy link
Member

@SeanCassiere Thank you again for your feedback. I've changed __key to __TSR_key.  If merging it for v2 is not too much trouble, I would be very happy to see it included.

@naoya7076 we currently do not have a v2 development branch open. As I mentioned earlier, I have added it onto the v2 list so that it gets done for its development.

The only path forward here, in having this merged into main at the moment, would be to have both key and __TSR_key be added in and have their functionality/values be synced during the v1 cycle. The key property would be marked as TODO: Remove in v2 using a comment. Then, when it comes time to begin working on v2, we'd go ahead and act on the TODO.

@naoya7076
Copy link
Author

@SeanCassiere Thank you for the guidance! I've updated the implementation as requested:

  • Added both key and __TSR_key properties with synced values
  • Marked key with TODO: Remove in v2 comments
  • Updated all affected packages and documentation

Thanks for the clear direction! 🙏

@naoya7076 naoya7076 requested a review from SeanCassiere June 17, 2025 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants