|
| 1 | +Welcome to your new TanStack app! |
| 2 | + |
| 3 | +# Getting Started |
| 4 | + |
| 5 | +To run this application: |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install |
| 9 | +npm run dev |
| 10 | +``` |
| 11 | + |
| 12 | +# Building For Production |
| 13 | + |
| 14 | +To build this application for production: |
| 15 | + |
| 16 | +```bash |
| 17 | +npm run build |
| 18 | +``` |
| 19 | + |
| 20 | +## Testing |
| 21 | + |
| 22 | +This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with: |
| 23 | + |
| 24 | +```bash |
| 25 | +npm run test |
| 26 | +``` |
| 27 | + |
| 28 | +## Styling |
| 29 | + |
| 30 | +This project uses [Tailwind CSS](https://tailwindcss.com/) for styling. |
| 31 | + |
| 32 | +## Linting & Formatting |
| 33 | + |
| 34 | +This project uses [Biome](https://biomejs.dev/) for linting and formatting. The following scripts are available: |
| 35 | + |
| 36 | +```bash |
| 37 | +npm run lint |
| 38 | +npm run format |
| 39 | +npm run check |
| 40 | +``` |
| 41 | + |
| 42 | +# TanStack Chat Application |
| 43 | + |
| 44 | +Am example chat application built with TanStack Start, TanStack Store, and Claude AI. |
| 45 | + |
| 46 | +## .env Updates |
| 47 | + |
| 48 | +```env |
| 49 | +ANTHROPIC_API_KEY=your_anthropic_api_key |
| 50 | +``` |
| 51 | + |
| 52 | +## ✨ Features |
| 53 | + |
| 54 | +### AI Capabilities |
| 55 | + |
| 56 | +- 🤖 Powered by Claude 3.5 Sonnet |
| 57 | +- 📝 Rich markdown formatting with syntax highlighting |
| 58 | +- 🎯 Customizable system prompts for tailored AI behavior |
| 59 | +- 🔄 Real-time message updates and streaming responses (coming soon) |
| 60 | + |
| 61 | +### User Experience |
| 62 | + |
| 63 | +- 🎨 Modern UI with Tailwind CSS and Lucide icons |
| 64 | +- 🔍 Conversation management and history |
| 65 | +- 🔐 Secure API key management |
| 66 | +- 📋 Markdown rendering with code highlighting |
| 67 | + |
| 68 | +### Technical Features |
| 69 | + |
| 70 | +- 📦 Centralized state management with TanStack Store |
| 71 | +- 🔌 Extensible architecture for multiple AI providers |
| 72 | +- 🛠️ TypeScript for type safety |
| 73 | + |
| 74 | +## Architecture |
| 75 | + |
| 76 | +### Tech Stack |
| 77 | + |
| 78 | +- **Frontend Framework**: TanStack Start |
| 79 | +- **Routing**: TanStack Router |
| 80 | +- **State Management**: TanStack Store |
| 81 | +- **Styling**: Tailwind CSS |
| 82 | +- **AI Integration**: Anthropic's Claude API |
| 83 | + |
| 84 | +## Shadcn |
| 85 | + |
| 86 | +Add components using the latest version of [Shadcn](https://ui.shadcn.com/). |
| 87 | + |
| 88 | +```bash |
| 89 | +pnpm dlx shadcn@latest add button |
| 90 | +``` |
| 91 | + |
| 92 | +## Routing |
| 93 | + |
| 94 | +This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as files in `src/routes`. |
| 95 | + |
| 96 | +### Adding A Route |
| 97 | + |
| 98 | +To add a new route to your application just add another a new file in the `./src/routes` directory. |
| 99 | + |
| 100 | +TanStack will automatically generate the content of the route file for you. |
| 101 | + |
| 102 | +Now that you have two routes you can use a `Link` component to navigate between them. |
| 103 | + |
| 104 | +### Adding Links |
| 105 | + |
| 106 | +To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`. |
| 107 | + |
| 108 | +```tsx |
| 109 | +import { Link } from '@tanstack/react-router' |
| 110 | +``` |
| 111 | + |
| 112 | +Then anywhere in your JSX you can use it like so: |
| 113 | + |
| 114 | +```tsx |
| 115 | +<Link to="/about">About</Link> |
| 116 | +``` |
| 117 | + |
| 118 | +This will create a link that will navigate to the `/about` route. |
| 119 | + |
| 120 | +More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent). |
| 121 | + |
| 122 | +### Using A Layout |
| 123 | + |
| 124 | +In the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you use the `<Outlet />` component. |
| 125 | + |
| 126 | +Here is an example layout that includes a header: |
| 127 | + |
| 128 | +```tsx |
| 129 | +import { Outlet, createRootRoute } from '@tanstack/react-router' |
| 130 | +import { TanStackRouterDevtools } from '@tanstack/react-router-devtools' |
| 131 | + |
| 132 | +import { Link } from '@tanstack/react-router' |
| 133 | + |
| 134 | +export const Route = createRootRoute({ |
| 135 | + component: () => ( |
| 136 | + <> |
| 137 | + <header> |
| 138 | + <nav> |
| 139 | + <Link to="/">Home</Link> |
| 140 | + <Link to="/about">About</Link> |
| 141 | + </nav> |
| 142 | + </header> |
| 143 | + <Outlet /> |
| 144 | + <TanStackRouterDevtools /> |
| 145 | + </> |
| 146 | + ), |
| 147 | +}) |
| 148 | +``` |
| 149 | + |
| 150 | +The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout. |
| 151 | + |
| 152 | +More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts). |
| 153 | + |
| 154 | +## Data Fetching |
| 155 | + |
| 156 | +There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered. |
| 157 | + |
| 158 | +For example: |
| 159 | + |
| 160 | +```tsx |
| 161 | +const peopleRoute = createRoute({ |
| 162 | + getParentRoute: () => rootRoute, |
| 163 | + path: '/people', |
| 164 | + loader: async () => { |
| 165 | + const response = await fetch('https://swapi.dev/api/people') |
| 166 | + return response.json() as Promise<{ |
| 167 | + results: { |
| 168 | + name: string |
| 169 | + }[] |
| 170 | + }> |
| 171 | + }, |
| 172 | + component: () => { |
| 173 | + const data = peopleRoute.useLoaderData() |
| 174 | + return ( |
| 175 | + <ul> |
| 176 | + {data.results.map((person) => ( |
| 177 | + <li key={person.name}>{person.name}</li> |
| 178 | + ))} |
| 179 | + </ul> |
| 180 | + ) |
| 181 | + }, |
| 182 | +}) |
| 183 | +``` |
| 184 | + |
| 185 | +Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters). |
| 186 | + |
| 187 | +### React-Query |
| 188 | + |
| 189 | +React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze. |
| 190 | + |
| 191 | +First add your dependencies: |
| 192 | + |
| 193 | +```bash |
| 194 | +npm install @tanstack/react-query @tanstack/react-query-devtools |
| 195 | +``` |
| 196 | + |
| 197 | +Next we'll need to create a query client and provider. We recommend putting those in `main.tsx`. |
| 198 | + |
| 199 | +```tsx |
| 200 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 201 | + |
| 202 | +// ... |
| 203 | + |
| 204 | +const queryClient = new QueryClient() |
| 205 | + |
| 206 | +// ... |
| 207 | + |
| 208 | +if (!rootElement.innerHTML) { |
| 209 | + const root = ReactDOM.createRoot(rootElement) |
| 210 | + |
| 211 | + root.render( |
| 212 | + <QueryClientProvider client={queryClient}> |
| 213 | + <RouterProvider router={router} /> |
| 214 | + </QueryClientProvider>, |
| 215 | + ) |
| 216 | +} |
| 217 | +``` |
| 218 | + |
| 219 | +You can also add TanStack Query Devtools to the root route (optional). |
| 220 | + |
| 221 | +```tsx |
| 222 | +import { ReactQueryDevtools } from '@tanstack/react-query-devtools' |
| 223 | + |
| 224 | +const rootRoute = createRootRoute({ |
| 225 | + component: () => ( |
| 226 | + <> |
| 227 | + <Outlet /> |
| 228 | + <ReactQueryDevtools buttonPosition="top-right" /> |
| 229 | + <TanStackRouterDevtools /> |
| 230 | + </> |
| 231 | + ), |
| 232 | +}) |
| 233 | +``` |
| 234 | + |
| 235 | +Now you can use `useQuery` to fetch your data. |
| 236 | + |
| 237 | +```tsx |
| 238 | +import { useQuery } from '@tanstack/react-query' |
| 239 | + |
| 240 | +import './App.css' |
| 241 | + |
| 242 | +function App() { |
| 243 | + const { data } = useQuery({ |
| 244 | + queryKey: ['people'], |
| 245 | + queryFn: () => |
| 246 | + fetch('https://swapi.dev/api/people') |
| 247 | + .then((res) => res.json()) |
| 248 | + .then((data) => data.results as { name: string }[]), |
| 249 | + initialData: [], |
| 250 | + }) |
| 251 | + |
| 252 | + return ( |
| 253 | + <div> |
| 254 | + <ul> |
| 255 | + {data.map((person) => ( |
| 256 | + <li key={person.name}>{person.name}</li> |
| 257 | + ))} |
| 258 | + </ul> |
| 259 | + </div> |
| 260 | + ) |
| 261 | +} |
| 262 | + |
| 263 | +export default App |
| 264 | +``` |
| 265 | + |
| 266 | +You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview). |
| 267 | + |
| 268 | +## State Management |
| 269 | + |
| 270 | +Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project. |
| 271 | + |
| 272 | +First you need to add TanStack Store as a dependency: |
| 273 | + |
| 274 | +```bash |
| 275 | +npm install @tanstack/store |
| 276 | +``` |
| 277 | + |
| 278 | +Now let's create a simple counter in the `src/App.tsx` file as a demonstration. |
| 279 | + |
| 280 | +```tsx |
| 281 | +import { useStore } from '@tanstack/react-store' |
| 282 | +import { Store } from '@tanstack/store' |
| 283 | +import './App.css' |
| 284 | + |
| 285 | +const countStore = new Store(0) |
| 286 | + |
| 287 | +function App() { |
| 288 | + const count = useStore(countStore) |
| 289 | + return ( |
| 290 | + <div> |
| 291 | + <button onClick={() => countStore.setState((n) => n + 1)}> |
| 292 | + Increment - {count} |
| 293 | + </button> |
| 294 | + </div> |
| 295 | + ) |
| 296 | +} |
| 297 | + |
| 298 | +export default App |
| 299 | +``` |
| 300 | + |
| 301 | +One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates. |
| 302 | + |
| 303 | +Let's check this out by doubling the count using derived state. |
| 304 | + |
| 305 | +```tsx |
| 306 | +import { useStore } from '@tanstack/react-store' |
| 307 | +import { Store, Derived } from '@tanstack/store' |
| 308 | +import './App.css' |
| 309 | + |
| 310 | +const countStore = new Store(0) |
| 311 | + |
| 312 | +const doubledStore = new Derived({ |
| 313 | + fn: () => countStore.state * 2, |
| 314 | + deps: [countStore], |
| 315 | +}) |
| 316 | +doubledStore.mount() |
| 317 | + |
| 318 | +function App() { |
| 319 | + const count = useStore(countStore) |
| 320 | + const doubledCount = useStore(doubledStore) |
| 321 | + |
| 322 | + return ( |
| 323 | + <div> |
| 324 | + <button onClick={() => countStore.setState((n) => n + 1)}> |
| 325 | + Increment - {count} |
| 326 | + </button> |
| 327 | + <div>Doubled - {doubledCount}</div> |
| 328 | + </div> |
| 329 | + ) |
| 330 | +} |
| 331 | + |
| 332 | +export default App |
| 333 | +``` |
| 334 | + |
| 335 | +We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating. |
| 336 | + |
| 337 | +Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook. |
| 338 | + |
| 339 | +You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest). |
| 340 | + |
| 341 | +# Demo files |
| 342 | + |
| 343 | +Files prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed. |
| 344 | + |
| 345 | +# Learn More |
| 346 | + |
| 347 | +You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com). |
0 commit comments