Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/react/time-travel/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-check

/** @type {import('eslint').Linter.Config} */
const config = {
settings: {
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'],
rules: {
'react/no-children-prop': 'off',
},
},
}

module.exports = config
27 changes: 27 additions & 0 deletions examples/react/time-travel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

pnpm-lock.yaml
yarn.lock
package-lock.json

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
6 changes: 6 additions & 0 deletions examples/react/time-travel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example

To run this example:

- `npm install`
- `npm run dev`
16 changes: 16 additions & 0 deletions examples/react/time-travel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/emblem-light.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<script src="https://unpkg.com/react-scan/dist/auto.global.js"></script>
<title>Basic Example - TanStack Devtools</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions examples/react/time-travel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@tanstack/devtools-example-react-time-travel",
"private": true,
"type": "module",
"scripts": {
"dev": "vite --port=3005",
"build": "vite build",
"preview": "vite preview",
"test:types": "tsc"
},
"dependencies": {
"@tanstack/devtools-event-client": "workspace:^",
"@tanstack/react-devtools": "^0.3.0",
"@tanstack/react-query": "^5.83.0",
"@tanstack/react-query-devtools": "^5.83.0",
"@tanstack/react-router": "^1.130.2",
"@tanstack/react-router-devtools": "^1.130.2",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"zod": "^4.0.14",
"zustand": "^5.0.7"
},
"devDependencies": {
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react": "^4.5.2",
"vite": "^7.0.6"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
13 changes: 13 additions & 0 deletions examples/react/time-travel/public/emblem-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions examples/react/time-travel/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createRoot } from 'react-dom/client'
import { useStore } from 'zustand'
import Devtools from './setup'
import { store } from './zustand-client'

function App() {
const { count, increment, decrement } = useStore(store)
return (
<div>
<h1>Zustand time-travel</h1>
<h2>Current count: {count}</h2>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
<Devtools />
</div>
)
}

const root = createRoot(document.getElementById('root')!)
root.render(<App />)
87 changes: 87 additions & 0 deletions examples/react/time-travel/src/setup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools'
import {
Link,
Outlet,
RouterProvider,
createRootRoute,
createRoute,
createRouter,
} from '@tanstack/react-router'
import { TanstackDevtools } from '@tanstack/react-devtools'
import { ZustandTimeTravel } from './zustand-time-travel'

const rootRoute = createRootRoute({
component: () => (
<>
<div className="p-2 flex gap-2">
<Link to="/" className="[&.active]:font-bold">
Home
</Link>{' '}
<Link to="/about" className="[&.active]:font-bold">
About
</Link>
</div>
<hr />
<Outlet />
</>
),
})

const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: function Index() {
return (
<div className="p-2">
<h3>Welcome Home!</h3>
</div>
)
},
})
function About() {
return (
<div className="p-2">
<h3>Hello from About!</h3>
</div>
)
}

const aboutRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/about',
component: About,
})

const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])

const router = createRouter({ routeTree })

const queryClient = new QueryClient()

export default function DevtoolsExample() {
return (
<>
<QueryClientProvider client={queryClient}>
<TanstackDevtools
plugins={[
{
name: 'Tanstack Query',
render: <ReactQueryDevtoolsPanel />,
},
{
name: 'Tanstack Router',
render: <TanStackRouterDevtoolsPanel router={router} />,
},
{
name: 'Zustand time-travel',
render: <ZustandTimeTravel />,
},
]}
/>
<RouterProvider router={router} />
</QueryClientProvider>
</>
)
}
36 changes: 36 additions & 0 deletions examples/react/time-travel/src/zustand-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { EventClient } from '@tanstack/devtools-event-client'
import { createStore } from 'zustand'

interface ZustandEventMap {
'zustand:stateChange': any
'zustand:revertSnapshot': any
}
export const eventClient = new EventClient<ZustandEventMap>({
pluginId: 'zustand',
})

export const store = createStore<{
count: number
increment: () => void
decrement: () => void
}>((set) => ({
count: 0,
increment: () => {
return set((state) => {
eventClient.emit('stateChange', { count: state.count + 1 })
return { count: state.count + 1 }
})
},
decrement: () => {
return set((state) => {
eventClient.emit('stateChange', { count: state.count - 1 })
return { count: state.count - 1 }
})
},
}))

eventClient.on('revertSnapshot', (snapshot) => {
store.setState({
count: snapshot.payload.count,
})
})
32 changes: 32 additions & 0 deletions examples/react/time-travel/src/zustand-time-travel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from 'react'
import { eventClient } from './zustand-client'

export function ZustandTimeTravel() {
const [snapshots, setSnapshots] = useState<Array<any>>([])

useEffect(() => {
const cleanup = eventClient.on('stateChange', (event) =>
setSnapshots((prev) => [...prev, event.payload]),
)
return () => {
cleanup()
}
}, [])

return (
<div>
{/* Snapshot slider to change the current state */}
Drag Me to time travel through zustand states
<hr />
<input
type="range"
min={0}
max={snapshots.length - 1}
onChange={(e) => {
const index = Number(e.target.value)
eventClient.emit('revertSnapshot', snapshots[index])
}}
/>
</div>
)
}
23 changes: 23 additions & 0 deletions examples/react/time-travel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "Bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src", "vite.config.ts"]
}
13 changes: 13 additions & 0 deletions examples/react/time-travel/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vite.dev/config/
export default defineConfig({
plugins: [
react({
// babel: {
// plugins: [['babel-plugin-react-compiler', { target: '19' }]],
// },
}),
],
})
Loading