router icon indicating copy to clipboard operation
router copied to clipboard

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

Open naoya7076 opened this issue 6 months ago • 3 comments

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:

https://github.com/TanStack/router/discussions/284#discussioncomment-9341612

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.

naoya7076 avatar Jun 10 '25 13:06 naoya7076

@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 avatar Jun 13 '25 05:06 naoya7076

@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.

SeanCassiere avatar Jun 13 '25 06:06 SeanCassiere

@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 avatar Jun 13 '25 13:06 naoya7076

View your CI Pipeline Execution ↗ for commit 5962a349f7cbc458f22a687888314abdcb6c53e8.

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 2m 2s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2025-06-23 02:47:53 UTC

nx-cloud[bot] avatar Jun 22 '25 23:06 nx-cloud[bot]

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@4356
@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@4356
@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@4356
@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@4356
@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@4356
@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@4356
@tanstack/react-router-with-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-with-query@4356
@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@4356
@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@4356
@tanstack/react-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@4356
@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@4356
@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@4356
@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@4356
@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@4356
@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@4356
@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@4356
@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@4356
@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@4356
@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@4356
@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@4356
@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@4356
@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@4356
@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@4356
@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@4356
@tanstack/solid-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@4356
@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@4356
@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@4356
@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@4356
@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@4356
@tanstack/start-server-functions-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-client@4356
@tanstack/start-server-functions-fetcher

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-fetcher@4356
@tanstack/start-server-functions-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@4356
@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@4356
@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@4356
@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@4356

commit: 5962a34

pkg-pr-new[bot] avatar Jun 22 '25 23:06 pkg-pr-new[bot]

@naoya7076 the only thing left to resolve is the failing test.

SeanCassiere avatar Jun 23 '25 01:06 SeanCassiere

I've updated the description and added the removal into the v2 checklist.

@naoya7076 thank you for taking this up and working with us on this one!

SeanCassiere avatar Jun 23 '25 02:06 SeanCassiere