create-start-app 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/create-app.js
CHANGED
|
@@ -178,6 +178,7 @@ async function copyFilesRecursively(environment, source, target, templateFile) {
|
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
export async function createApp(options, { silent = false, environment, }) {
|
|
181
|
+
environment.startRun();
|
|
181
182
|
const templateDirBase = fileURLToPath(new URL(`../templates/${options.framework}/base`, import.meta.url));
|
|
182
183
|
const templateDirRouter = fileURLToPath(new URL(`../templates/${options.framework}/${options.mode}`, import.meta.url));
|
|
183
184
|
const targetDir = resolve(process.cwd(), options.projectName);
|
|
@@ -379,6 +380,15 @@ export async function createApp(options, { silent = false, environment, }) {
|
|
|
379
380
|
await environment.execute('git', ['init'], targetDir);
|
|
380
381
|
s?.stop(`Initialized git repository`);
|
|
381
382
|
}
|
|
383
|
+
environment.finishRun();
|
|
384
|
+
let errorStatement = '';
|
|
385
|
+
if (environment.getErrors().length) {
|
|
386
|
+
errorStatement = `
|
|
387
|
+
|
|
388
|
+
${chalk.red('There were errors encountered during this process:')}
|
|
389
|
+
|
|
390
|
+
${environment.getErrors().join('\n')}`;
|
|
391
|
+
}
|
|
382
392
|
if (!silent) {
|
|
383
393
|
outro(`Created your new TanStack app in '${basename(targetDir)}'.
|
|
384
394
|
|
|
@@ -386,7 +396,6 @@ Use the following commands to start your app:
|
|
|
386
396
|
% cd ${options.projectName}
|
|
387
397
|
% ${options.packageManager === 'deno' ? 'deno start' : options.packageManager} ${isAddOnEnabled('start') ? 'dev' : 'start'}
|
|
388
398
|
|
|
389
|
-
Please read README.md for more information on testing, styling, adding routes, react-query, etc
|
|
390
|
-
`);
|
|
399
|
+
Please read README.md for more information on testing, styling, adding routes, react-query, etc.${errorStatement}`);
|
|
391
400
|
}
|
|
392
401
|
}
|
package/dist/environment.js
CHANGED
|
@@ -3,7 +3,13 @@ import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
|
3
3
|
import { dirname } from 'node:path';
|
|
4
4
|
import { execa } from 'execa';
|
|
5
5
|
export function createDefaultEnvironment() {
|
|
6
|
+
let errors = [];
|
|
6
7
|
return {
|
|
8
|
+
startRun: () => {
|
|
9
|
+
errors = [];
|
|
10
|
+
},
|
|
11
|
+
finishRun: () => { },
|
|
12
|
+
getErrors: () => errors,
|
|
7
13
|
appendFile: async (path, contents) => {
|
|
8
14
|
await mkdir(dirname(path), { recursive: true });
|
|
9
15
|
return appendFile(path, contents);
|
|
@@ -17,9 +23,14 @@ export function createDefaultEnvironment() {
|
|
|
17
23
|
return writeFile(path, contents);
|
|
18
24
|
},
|
|
19
25
|
execute: async (command, args, cwd) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
try {
|
|
27
|
+
await execa(command, args, {
|
|
28
|
+
cwd,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
errors.push(`Command "${command} ${args.join(' ')}" did not run successfully. Please run this manually in your project.`);
|
|
33
|
+
}
|
|
23
34
|
},
|
|
24
35
|
readFile: (path, encoding) => readFile(path, { encoding: encoding || 'utf8' }),
|
|
25
36
|
exists: (path) => existsSync(path),
|
package/package.json
CHANGED
package/src/create-app.ts
CHANGED
|
@@ -275,6 +275,8 @@ export async function createApp(
|
|
|
275
275
|
environment: Environment
|
|
276
276
|
},
|
|
277
277
|
) {
|
|
278
|
+
environment.startRun()
|
|
279
|
+
|
|
278
280
|
const templateDirBase = fileURLToPath(
|
|
279
281
|
new URL(`../templates/${options.framework}/base`, import.meta.url),
|
|
280
282
|
)
|
|
@@ -630,6 +632,17 @@ export async function createApp(
|
|
|
630
632
|
s?.stop(`Initialized git repository`)
|
|
631
633
|
}
|
|
632
634
|
|
|
635
|
+
environment.finishRun()
|
|
636
|
+
|
|
637
|
+
let errorStatement = ''
|
|
638
|
+
if (environment.getErrors().length) {
|
|
639
|
+
errorStatement = `
|
|
640
|
+
|
|
641
|
+
${chalk.red('There were errors encountered during this process:')}
|
|
642
|
+
|
|
643
|
+
${environment.getErrors().join('\n')}`
|
|
644
|
+
}
|
|
645
|
+
|
|
633
646
|
if (!silent) {
|
|
634
647
|
outro(`Created your new TanStack app in '${basename(targetDir)}'.
|
|
635
648
|
|
|
@@ -637,7 +650,6 @@ Use the following commands to start your app:
|
|
|
637
650
|
% cd ${options.projectName}
|
|
638
651
|
% ${options.packageManager === 'deno' ? 'deno start' : options.packageManager} ${isAddOnEnabled('start') ? 'dev' : 'start'}
|
|
639
652
|
|
|
640
|
-
Please read README.md for more information on testing, styling, adding routes, react-query, etc
|
|
641
|
-
`)
|
|
653
|
+
Please read README.md for more information on testing, styling, adding routes, react-query, etc.${errorStatement}`)
|
|
642
654
|
}
|
|
643
655
|
}
|
package/src/environment.ts
CHANGED
|
@@ -10,6 +10,10 @@ import { dirname } from 'node:path'
|
|
|
10
10
|
import { execa } from 'execa'
|
|
11
11
|
|
|
12
12
|
export type Environment = {
|
|
13
|
+
startRun: () => void
|
|
14
|
+
finishRun: () => void
|
|
15
|
+
getErrors: () => Array<string>
|
|
16
|
+
|
|
13
17
|
appendFile: (path: string, contents: string) => Promise<void>
|
|
14
18
|
copyFile: (from: string, to: string) => Promise<void>
|
|
15
19
|
writeFile: (path: string, contents: string) => Promise<void>
|
|
@@ -22,7 +26,14 @@ export type Environment = {
|
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
export function createDefaultEnvironment(): Environment {
|
|
29
|
+
let errors: Array<string> = []
|
|
25
30
|
return {
|
|
31
|
+
startRun: () => {
|
|
32
|
+
errors = []
|
|
33
|
+
},
|
|
34
|
+
finishRun: () => {},
|
|
35
|
+
getErrors: () => errors,
|
|
36
|
+
|
|
26
37
|
appendFile: async (path: string, contents: string) => {
|
|
27
38
|
await mkdir(dirname(path), { recursive: true })
|
|
28
39
|
return appendFile(path, contents)
|
|
@@ -36,9 +47,15 @@ export function createDefaultEnvironment(): Environment {
|
|
|
36
47
|
return writeFile(path, contents)
|
|
37
48
|
},
|
|
38
49
|
execute: async (command: string, args: Array<string>, cwd: string) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
try {
|
|
51
|
+
await execa(command, args, {
|
|
52
|
+
cwd,
|
|
53
|
+
})
|
|
54
|
+
} catch {
|
|
55
|
+
errors.push(
|
|
56
|
+
`Command "${command} ${args.join(' ')}" did not run successfully. Please run this manually in your project.`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
42
59
|
},
|
|
43
60
|
|
|
44
61
|
readFile: (path: string, encoding?: BufferEncoding) =>
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"/src/routes/__root.tsx": "import { createRootRoute, Outlet } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/router-devtools'\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n",
|
|
15
15
|
"/src/routes/index.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport logo from '../logo.svg'\nimport '../App.css'\n\nexport const Route = createFileRoute('/')({\n component: App,\n})\n\nfunction App() {\n return (\n <div className=\"App\">\n <header className=\"App-header\">\n <img src={logo} className=\"App-logo\" alt=\"logo\" />\n <p>\n Edit <code>src/routes/index.tsx</code> and save to reload.\n </p>\n <a\n className=\"App-link\"\n href=\"https://reactjs.org\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn React\n </a>\n <a\n className=\"App-link\"\n href=\"https://tanstack.com\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn TanStack\n </a>\n </header>\n </div>\n )\n}\n",
|
|
16
16
|
"/Users/jherr/projects/cta/create-tsrouter-app/.gitignore": "node_modules\n.DS_Store\ndist\ndist-ssr\n*.local\n",
|
|
17
|
-
"/Users/jherr/projects/cta/create-tsrouter-app/README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm start\n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses CSS for styling.\n\n\n\n\n\n## Routing\nThis project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as
|
|
17
|
+
"/Users/jherr/projects/cta/create-tsrouter-app/README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm start\n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses CSS for styling.\n\n\n\n\n\n## Routing\nThis 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`.\n\n### Adding A Route\n\nTo add a new route to your application just add another a new file in the `./src/routes` directory.\n\nTanStack will automatically generate the content of the route file for you.\n\nNow that you have two routes you can use a `Link` component to navigate between them.\n\n### Adding Links\n\nTo use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.\n\n```tsx\nimport { Link } from \"@tanstack/react-router\";\n```\n\nThen anywhere in your JSX you can use it like so:\n\n```tsx\n<Link to=\"/about\">About</Link>\n```\n\nThis will create a link that will navigate to the `/about` route.\n\nMore information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).\n\n### Using A Layout\n\nIn 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.\n\nHere is an example layout that includes a header:\n\n```tsx\nimport { createRootRoute, Outlet } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/router-devtools'\n\nimport { Link } from \"@tanstack/react-router\";\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <header>\n <nav>\n <Link to=\"/\">Home</Link>\n <Link to=\"/about\">About</Link>\n </nav>\n </header>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n```\n\nThe `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.\n\nMore information on layouts can be found in the [Layouts documentation](hthttps://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).\n\n\n## Data Fetching\n\nThere 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.\n\nFor example:\n\n```tsx\nconst peopleRoute = createRoute({\n getParentRoute: () => rootRoute,\n path: \"/people\",\n loader: async () => {\n const response = await fetch(\"https://swapi.dev/api/people\");\n return response.json() as Promise<{\n results: {\n name: string;\n }[];\n }>;\n },\n component: () => {\n const data = peopleRoute.useLoaderData();\n return (\n <ul>\n {data.results.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n );\n },\n});\n```\n\nLoaders 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).\n\n### React-Query\n\nReact-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.\n\nFirst add your dependencies:\n\n```bash\nnpm install @tanstack/react-query @tanstack/react-query-devtools\n```\n\nNext we'll need to creata query client and provider. We recommend putting those in `main.tsx`.\n\n```tsx\nimport { QueryClient, QueryClientProvider } from \"@tanstack/react-query\";\n\n// ...\n\nconst queryClient = new QueryClient();\n\n// ...\n\nif (!rootElement.innerHTML) {\n const root = ReactDOM.createRoot(rootElement);\n\n root.render(\n <QueryClientProvider client={queryClient}>\n <RouterProvider router={router} />\n </QueryClientProvider>\n );\n}\n```\n\nYou can also add TanStack Query Devtools to the root route (optional).\n\n```tsx\nimport { ReactQueryDevtools } from \"@tanstack/react-query-devtools\";\n\nconst rootRoute = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <ReactQueryDevtools buttonPosition=\"top-right\" />\n <TanStackRouterDevtools />\n </>\n ),\n});\n```\n\nNow you can use `useQuery` to fetch your data.\n\n```tsx\nimport { useQuery } from \"@tanstack/react-query\";\n\nimport \"./App.css\";\n\nfunction App() {\n const { data } = useQuery({\n queryKey: [\"people\"],\n queryFn: () =>\n fetch(\"https://swapi.dev/api/people\")\n .then((res) => res.json())\n .then((data) => data.results as { name: string }[]),\n initialData: [],\n });\n\n return (\n <div>\n <ul>\n {data.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n </div>\n );\n}\n\nexport default App;\n```\n\nYou 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).\n\n## State Management\n\nAnother 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.\n\nFirst you need to add TanStack Store as a dependency:\n\n```bash\nnpm install @tanstack/store\n```\n\nNow let's create a simple counter in the `src/App.tsx` file as a demonstration.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nfunction App() {\n const count = useStore(countStore);\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n </div>\n );\n}\n\nexport default App;\n```\n\nOne 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.\n\nLet's check this out by doubling the count using derived state.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store, Derived } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nconst doubledStore = new Derived({\n fn: () => countStore.state * 2,\n deps: [countStore],\n});\ndoubledStore.mount();\n\nfunction App() {\n const count = useStore(countStore);\n const doubledCount = useStore(doubledStore);\n\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n <div>Doubled - {doubledCount}</div>\n </div>\n );\n}\n\nexport default App;\n```\n\nWe 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.\n\nOnce 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.\n\nYou can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).\n\n# Demo files\n\nFiles 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.\n\n# Learn More\n\nYou can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).\n"
|
|
18
18
|
},
|
|
19
19
|
"commands": [
|
|
20
20
|
{
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"/src/routes/__root.tsx": "import { createRootRoute, Outlet } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/router-devtools'\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n",
|
|
14
14
|
"/src/routes/index.tsx": "import { createFileRoute } from '@tanstack/react-router'\nimport logo from '../logo.svg'\n\nexport const Route = createFileRoute('/')({\n component: App,\n})\n\nfunction App() {\n return (\n <div className=\"text-center\">\n <header className=\"min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]\">\n <img\n src={logo}\n className=\"h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]\"\n alt=\"logo\"\n />\n <p>\n Edit <code>src/routes/index.tsx</code> and save to reload.\n </p>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://reactjs.org\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn React\n </a>\n <a\n className=\"text-[#61dafb] hover:underline\"\n href=\"https://tanstack.com\"\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n Learn TanStack\n </a>\n </header>\n </div>\n )\n}\n",
|
|
15
15
|
"/Users/jherr/projects/cta/create-tsrouter-app/.gitignore": "node_modules\n.DS_Store\ndist\ndist-ssr\n*.local\n",
|
|
16
|
-
"/Users/jherr/projects/cta/create-tsrouter-app/README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm start\n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses [Tailwind CSS](https://tailwindcss.com/) for styling.\n\n\n\n\n\n## Routing\nThis project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as
|
|
16
|
+
"/Users/jherr/projects/cta/create-tsrouter-app/README.md": "Welcome to your new TanStack app! \n\n# Getting Started\n\nTo run this application:\n\n```bash\nnpm install\nnpm start\n```\n\n# Building For Production\n\nTo build this application for production:\n\n```bash\nnpm run build\n```\n\n## Testing\n\nThis project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:\n\n```bash\nnpm run test\n```\n\n## Styling\n\nThis project uses [Tailwind CSS](https://tailwindcss.com/) for styling.\n\n\n\n\n\n## Routing\nThis 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`.\n\n### Adding A Route\n\nTo add a new route to your application just add another a new file in the `./src/routes` directory.\n\nTanStack will automatically generate the content of the route file for you.\n\nNow that you have two routes you can use a `Link` component to navigate between them.\n\n### Adding Links\n\nTo use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.\n\n```tsx\nimport { Link } from \"@tanstack/react-router\";\n```\n\nThen anywhere in your JSX you can use it like so:\n\n```tsx\n<Link to=\"/about\">About</Link>\n```\n\nThis will create a link that will navigate to the `/about` route.\n\nMore information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).\n\n### Using A Layout\n\nIn 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.\n\nHere is an example layout that includes a header:\n\n```tsx\nimport { createRootRoute, Outlet } from '@tanstack/react-router'\nimport { TanStackRouterDevtools } from '@tanstack/router-devtools'\n\nimport { Link } from \"@tanstack/react-router\";\n\nexport const Route = createRootRoute({\n component: () => (\n <>\n <header>\n <nav>\n <Link to=\"/\">Home</Link>\n <Link to=\"/about\">About</Link>\n </nav>\n </header>\n <Outlet />\n <TanStackRouterDevtools />\n </>\n ),\n})\n```\n\nThe `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.\n\nMore information on layouts can be found in the [Layouts documentation](hthttps://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).\n\n\n## Data Fetching\n\nThere 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.\n\nFor example:\n\n```tsx\nconst peopleRoute = createRoute({\n getParentRoute: () => rootRoute,\n path: \"/people\",\n loader: async () => {\n const response = await fetch(\"https://swapi.dev/api/people\");\n return response.json() as Promise<{\n results: {\n name: string;\n }[];\n }>;\n },\n component: () => {\n const data = peopleRoute.useLoaderData();\n return (\n <ul>\n {data.results.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n );\n },\n});\n```\n\nLoaders 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).\n\n### React-Query\n\nReact-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.\n\nFirst add your dependencies:\n\n```bash\nnpm install @tanstack/react-query @tanstack/react-query-devtools\n```\n\nNext we'll need to creata query client and provider. We recommend putting those in `main.tsx`.\n\n```tsx\nimport { QueryClient, QueryClientProvider } from \"@tanstack/react-query\";\n\n// ...\n\nconst queryClient = new QueryClient();\n\n// ...\n\nif (!rootElement.innerHTML) {\n const root = ReactDOM.createRoot(rootElement);\n\n root.render(\n <QueryClientProvider client={queryClient}>\n <RouterProvider router={router} />\n </QueryClientProvider>\n );\n}\n```\n\nYou can also add TanStack Query Devtools to the root route (optional).\n\n```tsx\nimport { ReactQueryDevtools } from \"@tanstack/react-query-devtools\";\n\nconst rootRoute = createRootRoute({\n component: () => (\n <>\n <Outlet />\n <ReactQueryDevtools buttonPosition=\"top-right\" />\n <TanStackRouterDevtools />\n </>\n ),\n});\n```\n\nNow you can use `useQuery` to fetch your data.\n\n```tsx\nimport { useQuery } from \"@tanstack/react-query\";\n\nimport \"./App.css\";\n\nfunction App() {\n const { data } = useQuery({\n queryKey: [\"people\"],\n queryFn: () =>\n fetch(\"https://swapi.dev/api/people\")\n .then((res) => res.json())\n .then((data) => data.results as { name: string }[]),\n initialData: [],\n });\n\n return (\n <div>\n <ul>\n {data.map((person) => (\n <li key={person.name}>{person.name}</li>\n ))}\n </ul>\n </div>\n );\n}\n\nexport default App;\n```\n\nYou 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).\n\n## State Management\n\nAnother 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.\n\nFirst you need to add TanStack Store as a dependency:\n\n```bash\nnpm install @tanstack/store\n```\n\nNow let's create a simple counter in the `src/App.tsx` file as a demonstration.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nfunction App() {\n const count = useStore(countStore);\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n </div>\n );\n}\n\nexport default App;\n```\n\nOne 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.\n\nLet's check this out by doubling the count using derived state.\n\n```tsx\nimport { useStore } from \"@tanstack/react-store\";\nimport { Store, Derived } from \"@tanstack/store\";\nimport \"./App.css\";\n\nconst countStore = new Store(0);\n\nconst doubledStore = new Derived({\n fn: () => countStore.state * 2,\n deps: [countStore],\n});\ndoubledStore.mount();\n\nfunction App() {\n const count = useStore(countStore);\n const doubledCount = useStore(doubledStore);\n\n return (\n <div>\n <button onClick={() => countStore.setState((n) => n + 1)}>\n Increment - {count}\n </button>\n <div>Doubled - {doubledCount}</div>\n </div>\n );\n}\n\nexport default App;\n```\n\nWe 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.\n\nOnce 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.\n\nYou can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).\n\n# Demo files\n\nFiles 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.\n\n# Learn More\n\nYou can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).\n"
|
|
17
17
|
},
|
|
18
18
|
"commands": [
|
|
19
19
|
{
|