Skip to content
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

feat(react-router): add useCanGoBack #3017

Merged
merged 13 commits into from
Jan 3, 2025
Merged

Conversation

tomrehnstrom
Copy link
Contributor

@tomrehnstrom tomrehnstrom commented Dec 15, 2024

fixes #2983

Copy link

nx-cloud bot commented Dec 15, 2024

View your CI Pipeline Execution ↗ for commit d471ca4.

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

☁️ Nx Cloud last updated this comment at 2025-01-03 20:08:00 UTC

Copy link

pkg-pr-new bot commented Dec 15, 2024

Open in Stackblitz

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@3017

@tanstack/create-router

npm i https://pkg.pr.new/@tanstack/create-router@3017

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@3017

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@3017

@tanstack/react-cross-context

npm i https://pkg.pr.new/@tanstack/react-cross-context@3017

@tanstack/create-start

npm i https://pkg.pr.new/@tanstack/create-start@3017

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@3017

@tanstack/react-router-with-query

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

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@3017

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@3017

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@3017

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@3017

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@3017

@tanstack/start

npm i https://pkg.pr.new/@tanstack/start@3017

@tanstack/start-vite-plugin

npm i https://pkg.pr.new/@tanstack/start-vite-plugin@3017

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@3017

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@3017

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@3017

commit: d471ca4

// This is a fallback for when updating the router and there are history entries without index
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const currentIndex = location.state.index ?? 0
state = assignKey({ ...state, index: currentIndex + 1 })
Copy link
Contributor

Choose a reason for hiding this comment

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

can we put this logic into assigKey()?

// This is a fallback for when updating the router and there are history entries without index
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const currentIndex = location.state.index ?? 0
state = assignKey({ ...state, index: currentIndex })
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above, can we put this logic into assigKey()?

@@ -551,6 +568,7 @@ export function createMemoryHistory(
entries[index] = path
},
back: () => {
console.log('back', index)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

@@ -51,17 +52,12 @@ export interface ParsedPath {

export interface HistoryState {
key?: string
index: number
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be index?: number as the index is not set for the initial entry?

also, i think we should use a more ... obscure name to not collide with user data

e.g. __TSR_index ?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's always available after we run through parseHref, this is because, if the route has no state (should be valid only for initial route), we default it to { index: 0 }. About the naming, sure, but by that logic, shouldn't the key also be renamed?

Copy link
Contributor

Choose a reason for hiding this comment

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

ideally, yes. but the key has been there since the beginning, but adding an index now could introduce a breaking change

return { router }
}

describe('history: History gives correct notifcations and state', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
describe('history: History gives correct notifcations and state', () => {
describe('history: History gives correct notifications and state', () => {

})

const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
Copy link
Contributor

Choose a reason for hiding this comment

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

we should run the same test for all history types

subscriber({ location, action: { type: 'ROLLBACK' } }),
)
const handleIndexChange = (action: SubscriberHistoryAction) => {
if (opts.notifyOnIndexChange ?? true) notify(action)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you please always use { } even for one liners?

@@ -375,9 +377,10 @@ export function createBrowserHistory(opts?: {
}
}

const onPushPop = () => {
// NOTE: this function can probably be removed
Copy link
Contributor

Choose a reason for hiding this comment

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

so can we remove it?

@schiller-manuel schiller-manuel changed the title fix: ensured that history does not notify twice for certain actions and has index in state feat(react-router): add useCanGoBack Jan 3, 2025
@schiller-manuel schiller-manuel merged commit 366848e into TanStack:main Jan 3, 2025
5 checks passed
@MTG2000
Copy link

MTG2000 commented Jan 6, 2025

Small question.

Is this new __TSR_index property in HistoryState expected to be manually set every time we set a value for the state property on navigate, <Link>, ...etc?

Cause after updating my router version I started getting this error:

navigate({ state: { foo: "bar" } })
^
"Type '{ foo: "bar" }' is not assignable to type 'true | NonNullableUpdater<HistoryState> | undefined'.
  Property '__TSR_index' is missing in type '{ key: string; }' but required in type 'HistoryState'.ts(2322)"

Should I manually assign it with some value (like 0), or is there a better way?

@schiller-manuel
Copy link
Contributor

no this is meant to be automatically set

@ferretwithaberet we need to make it optional

@ccapndave
Copy link
Contributor

After updating I'm getting this error all over my code too.

 Property '__TSR_index' is missing in type '{ stackId: string; }' but required in type 'HistoryState'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

View Transition API not working for router.history navigation calls
5 participants