Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

README.md

328 lines8.5 KB

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