Skip to content

Commit d7f2575

Browse files
committed
fix: returning API requests to Start example
1 parent 66faa92 commit d7f2575

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createAPIFileRoute } from '@tanstack/react-start/api'
2+
3+
export const APIRoute = createAPIFileRoute('/demo/start/api/names')({
4+
GET: async ({ request }) => {
5+
return new Response(JSON.stringify(['Alice', 'Bob', 'Charlie']), {
6+
headers: {
7+
'Content-Type': 'application/json',
8+
},
9+
})
10+
},
11+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<% if (addOnEnabled['react-query']) { %>
2+
import { useQuery } from '@tanstack/react-query'
3+
<% } else { %>
4+
import { useEffect, useState } from 'react'
5+
<% } %>
6+
import { createFileRoute } from '@tanstack/react-router'
7+
8+
function getNames() {
9+
return fetch('/api/demo-names').then((res) => res.json())
10+
}
11+
12+
export const Route = createFileRoute('/demo/start/api-request')({
13+
component: Home,
14+
})
15+
16+
function Home() {
17+
<% if (addOnEnabled['react-query']) { %>
18+
const { data: names = [] } = useQuery({
19+
queryKey: ['names'],
20+
queryFn: getNames,
21+
})
22+
<% } else { %>
23+
const [names, setNames] = useState<Array<string>>([])
24+
useEffect(() => {
25+
getNames().then(setNames)
26+
}, [])
27+
<% } %>
28+
return (
29+
<div className="p-4">
30+
<div>{names.join(', ')}</div>
31+
</div>
32+
)
33+
}

templates/react/add-on/start/info.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
{
1010
"url": "/demo/start/server-funcs",
1111
"name": "Start - Server Functions"
12+
},
13+
{
14+
"url": "/demo/start/api-request",
15+
"name": "Start - API Request"
1216
}
1317
]
1418
}

0 commit comments

Comments
 (0)