-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ref: switch from throwing promises to React.use #10366
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
41ea6d9
feat: switch to `use` instead of throwing promises
TkDodo 9dfe64f
merge main
TkDodo 8a5fc2d
use
TkDodo 08fa02a
Merge branch 'main' into feature/use
TkDodo 5ff22cd
ci: apply automated fixes
autofix-ci[bot] 1392061
fix: revert unnecessary changes
TkDodo 8510102
fix: enhance error handling and observer updates in fetchOptimistic
TkDodo 16950f0
ssr test
TkDodo 4315a70
ci: apply automated fixes
autofix-ci[bot] e2cbc75
tanstack start integration test
TkDodo a5fec0f
ci: apply automated fixes
autofix-ci[bot] e6dfad8
more tests
TkDodo 29dbdb5
fix: allow observer.promise when suspense is enabled
TkDodo 28891ce
ref: unify suspense handling
TkDodo 700a102
Merge remote-tracking branch 'origin/main' into feature/use
TkDodo fcea8f9
ci: apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| dist | ||
| .vinxi | ||
| .output | ||
| src/routeTree.gen.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "react-start-integration", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev", | ||
| "build": "vite build" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/react-query": "workspace:*", | ||
| "@tanstack/react-router": "^1.166.1", | ||
| "@tanstack/react-router-ssr-query": "^1.166.1", | ||
| "@tanstack/react-start": "^1.166.1", | ||
| "react": "^19.2.1", | ||
| "react-dom": "^19.2.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^22.15.3", | ||
| "@types/react": "^19.2.7", | ||
| "@types/react-dom": "^19.2.3", | ||
| "@vitejs/plugin-react": "^4.3.4", | ||
| "typescript": "5.9.3", | ||
| "vite": "^6.4.1" | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
integrations/react-start/src/components/default-catch-boundary.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { | ||
| ErrorComponent, | ||
| Link, | ||
| rootRouteId, | ||
| useMatch, | ||
| useRouter, | ||
| } from '@tanstack/react-router' | ||
| import type { ErrorComponentProps } from '@tanstack/react-router' | ||
|
|
||
| export function DefaultCatchBoundary({ error }: ErrorComponentProps) { | ||
| const router = useRouter() | ||
| const isRoot = useMatch({ | ||
| strict: false, | ||
| select: (state) => state.id === rootRouteId, | ||
| }) | ||
|
|
||
| console.error(error) | ||
|
|
||
| return ( | ||
| <div className="min-w-0 flex-1 p-4 flex flex-col items-center justify-center gap-6"> | ||
| <ErrorComponent error={error} /> | ||
| <div className="flex gap-2 items-center flex-wrap"> | ||
| <button | ||
| onClick={() => { | ||
| router.invalidate() | ||
| }} | ||
| className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded-sm text-white uppercase font-extrabold`} | ||
| > | ||
| Try Again | ||
| </button> | ||
| {isRoot ? ( | ||
| <Link | ||
| to="/" | ||
| className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded-sm text-white uppercase font-extrabold`} | ||
| > | ||
| Home | ||
| </Link> | ||
| ) : ( | ||
| <Link | ||
| to="/" | ||
| className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded-sm text-white uppercase font-extrabold`} | ||
| onClick={(e) => { | ||
| e.preventDefault() | ||
| window.history.back() | ||
| }} | ||
| > | ||
| Go Back | ||
| </Link> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { Link } from '@tanstack/react-router' | ||
| import type { ReactNode } from 'react' | ||
|
|
||
| export function NotFound({ children }: { children?: ReactNode }) { | ||
| return ( | ||
| <div className="space-y-2 p-2"> | ||
| <div className="text-gray-600 dark:text-gray-400"> | ||
| {children || <p>The page you are looking for does not exist.</p>} | ||
| </div> | ||
| <p className="flex items-center gap-2 flex-wrap"> | ||
| <button | ||
| onClick={() => window.history.back()} | ||
| className="bg-emerald-500 text-white px-2 py-1 rounded-sm uppercase font-black text-sm" | ||
| > | ||
| Go back | ||
| </button> | ||
| <Link | ||
| to="/" | ||
| className="bg-cyan-600 text-white px-2 py-1 rounded-sm uppercase font-black text-sm" | ||
| > | ||
| Start Over | ||
| </Link> | ||
| </p> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
| import { createRouter } from '@tanstack/react-router' | ||
| import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query' | ||
| import { DefaultCatchBoundary } from './components/default-catch-boundary' | ||
| import { NotFound } from './components/not-found' | ||
| import { routeTree } from './routeTree.gen' | ||
|
|
||
| export function getRouter() { | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: false, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const router = createRouter({ | ||
| routeTree, | ||
| context: { queryClient }, | ||
| defaultPreload: 'intent', | ||
| defaultErrorComponent: DefaultCatchBoundary, | ||
| defaultNotFoundComponent: () => <NotFound />, | ||
| Wrap: ({ children }) => ( | ||
| <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
| ), | ||
| }) | ||
|
|
||
| setupRouterSsrQueryIntegration({ | ||
| queryClient, | ||
| router, | ||
| wrapQueryClient: false, | ||
| }) | ||
|
|
||
| return router | ||
| } | ||
|
|
||
| declare module '@tanstack/react-router' { | ||
| interface Register { | ||
| router: ReturnType<typeof getRouter> | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /// <reference types="vite/client" /> | ||
|
|
||
| import { | ||
| HeadContent, | ||
| Outlet, | ||
| Scripts, | ||
| createRootRouteWithContext, | ||
| } from '@tanstack/react-router' | ||
| import type { QueryClient } from '@tanstack/react-query' | ||
| import type { ReactNode } from 'react' | ||
|
|
||
| export const Route = createRootRouteWithContext<{ | ||
| queryClient: QueryClient | ||
| }>()({ | ||
| component: RootComponent, | ||
| }) | ||
|
|
||
| function RootComponent() { | ||
| return ( | ||
| <RootDocument> | ||
| <Outlet /> | ||
| </RootDocument> | ||
| ) | ||
| } | ||
|
|
||
| function RootDocument({ children }: Readonly<{ children: ReactNode }>) { | ||
| return ( | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charSet="utf-8" /> | ||
| <meta content="width=device-width, initial-scale=1" name="viewport" /> | ||
| <HeadContent /> | ||
| </head> | ||
| <body> | ||
| {children} | ||
| <Scripts /> | ||
| </body> | ||
| </html> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { queryOptions, useSuspenseQuery } from '@tanstack/react-query' | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import * as React from 'react' | ||
|
|
||
| type Profile = { | ||
| isDefault: boolean | ||
| profileId: string | ||
| } | ||
|
|
||
| const profilesQueryOptions = queryOptions({ | ||
| queryKey: ['profiles'], | ||
| queryFn: async (): Promise<Array<Profile>> => { | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
|
|
||
| return [ | ||
| { profileId: 'default-profile', isDefault: true }, | ||
| { profileId: 'secondary-profile', isDefault: false }, | ||
| ] | ||
| }, | ||
| retry: false, | ||
| }) | ||
|
|
||
| export const Route = createFileRoute('/')({ | ||
| component: Home, | ||
| }) | ||
|
|
||
| function Home() { | ||
| return ( | ||
| <main> | ||
| <h1>Profiles</h1> | ||
| <React.Suspense fallback={<p data-testid="loading">loading</p>}> | ||
| <ActiveProfile /> | ||
| </React.Suspense> | ||
| </main> | ||
| ) | ||
| } | ||
|
|
||
| function ActiveProfile() { | ||
| const profiles = useSuspenseQuery(profilesQueryOptions).data | ||
| const activeProfile = profiles.find((profile) => profile.isDefault) | ||
|
|
||
| return ( | ||
| <p data-testid="active-profile">{activeProfile?.profileId ?? 'missing'}</p> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "jsx": "react-jsx", | ||
| "lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
| "module": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "noEmit": true, | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "target": "ES2022", | ||
| "types": ["node", "vite/client"] | ||
| }, | ||
| "include": ["src/**/*.ts", "src/**/*.tsx", "vite.config.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineConfig } from 'vite' | ||
| import viteReact from '@vitejs/plugin-react' | ||
| import { tanstackStart } from '@tanstack/react-start/plugin/vite' | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| host: '127.0.0.1', | ||
| port: 3117, | ||
| }, | ||
| plugins: [tanstackStart(), viteReact()], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.