Skip to content

feat(react-router): add useCanGoBack - #3017

Merged
schiller-manuel merged 13 commits into
TanStack:mainfrom
tomrehnstrom:main
Jan 3, 2025
Merged

schiller-manuel merged 13 commits into
TanStack:mainfrom
tomrehnstrom:main

Conversation

@tomrehnstrom

@tomrehnstrom tomrehnstrom commented Dec 15, 2024 •

Copy link
Copy Markdown
Contributor

fixes #2983

@nx-cloud

nx-cloud Bot commented Dec 15, 2024 •

Copy link
Copy Markdown
Contributor

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

@pkg-pr-new

pkg-pr-new Bot commented Dec 15, 2024 •

Copy link
Copy Markdown

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

Comment thread packages/history/src/index.ts Outdated
// 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
Copy Markdown
Collaborator

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()?

Comment thread packages/history/src/index.ts Outdated
// 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
Copy Markdown
Collaborator

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()?

Comment thread packages/history/src/index.ts Outdated
entries[index] = path
},
back: () => {
console.log('back', index)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove

Comment thread packages/history/src/index.ts Outdated

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

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', () => {

Copy link
Copy Markdown
Collaborator

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

Comment thread packages/history/src/index.ts Outdated

Copy link
Copy Markdown
Collaborator

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?

Comment thread packages/history/src/index.ts Outdated

Copy link
Copy Markdown
Collaborator

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

MTG2000 commented Jan 6, 2025

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

no this is meant to be automatically set

@ferretwithaberet we need to make it optional

@ccapndave

Copy link
Copy Markdown
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'.

@igor9silva

Copy link
Copy Markdown

Hi, sorry to bother.

Just wanted to say I love you guys. Your working is amazing.

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

6 participants