-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: Preact Adapter #9935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Preact Adapter #9935
Changes from all commits
3daff38
0e40281
722e05b
78cf64a
a9c5c75
8cc6ed5
088795b
0f5bdbc
6d23454
60502ad
f2f73f9
460919e
1a3afe6
05af042
4d5a0f0
f451435
b2e95fd
34f0262
4e366c4
3c6b541
66605b1
c069be8
445e37b
bdc5d11
3e43af5
f1b1b78
9c72beb
893b99c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: graphql | ||
| title: GraphQL | ||
| ref: docs/framework/react/graphql.md | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: background-fetching-indicators | ||
| title: Background Fetching Indicators | ||
| ref: docs/framework/react/guides/background-fetching-indicators.md | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: caching | ||
| title: Caching Examples | ||
| ref: docs/framework/react/guides/caching.md | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: default-query-function | ||
| title: Default Query Function | ||
| ref: docs/framework/react/guides/default-query-function.md | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: dependent-queries | ||
| title: Dependent Queries | ||
| ref: docs/framework/react/guides/dependent-queries.md | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||
| --- | ||||||
| id: disabling-queries | ||||||
| title: Disabling/Pausing Queries | ||||||
| ref: docs/framework/react/guides/disabling-queries.md | ||||||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||||||
| --- | ||||||
|
|
||||||
| [//]: # 'Example2' | ||||||
|
|
||||||
| ```tsx | ||||||
| function Todos() { | ||||||
| const [filter, setFilter] = useState('') | ||||||
|
|
||||||
| const { data } = useQuery({ | ||||||
| queryKey: ['todos', filter], | ||||||
| queryFn: () => fetchTodos(filter), | ||||||
| // ⬇️ disabled as long as the filter is empty | ||||||
| enabled: !!filter, | ||||||
| }) | ||||||
|
|
||||||
| return ( | ||||||
| <div> | ||||||
| // 🚀 applying the filter will enable and execute the query | ||||||
| <FiltersForm onApply={setFilter} /> | ||||||
| {data && <TodosTable data={data} />} | ||||||
| </div> | ||||||
| ) | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| [//]: # 'Example2' | ||||||
|
|
||||||
| [//]: # 'Example3' | ||||||
|
|
||||||
| ```tsx | ||||||
| import { skipToken, useQuery } from '@tanstack/react-query' | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect package import in code example. Line 36 imports from 🔧 Proposed fix-import { skipToken, useQuery } from '@tanstack/react-query'
+import { skipToken, useQuery } from '@tanstack/preact-query'📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| function Todos() { | ||||||
| const [filter, setFilter] = useState<string | undefined>() | ||||||
|
|
||||||
| const { data } = useQuery({ | ||||||
| queryKey: ['todos', filter], | ||||||
| // ⬇️ disabled as long as the filter is undefined or empty | ||||||
| queryFn: filter ? () => fetchTodos(filter) : skipToken, | ||||||
| }) | ||||||
|
|
||||||
| return ( | ||||||
| <div> | ||||||
| // 🚀 applying the filter will enable and execute the query | ||||||
| <FiltersForm onApply={setFilter} /> | ||||||
| {data && <TodosTable data={data} />} | ||||||
| </div> | ||||||
| ) | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| [//]: # 'Example3' | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: does-this-replace-client-state | ||
| title: Does TanStack Query replace Redux, MobX or other global state managers? | ||
| ref: docs/framework/react/guides/does-this-replace-client-state | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: filters | ||
| title: Filters | ||
| ref: docs/framework/react/guides/filters | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: important-defaults | ||
| title: Important Defaults | ||
| ref: docs/framework/react/guides/important-defaults | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||||||
| --- | ||||||||||||
| id: infinite-queries | ||||||||||||
| title: Infinite Queries | ||||||||||||
| ref: docs/framework/react/guides/infinite-queries | ||||||||||||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||||||||||||
| --- | ||||||||||||
|
|
||||||||||||
| [//]: # 'Example' | ||||||||||||
|
|
||||||||||||
| ```tsx | ||||||||||||
| import { useInfiniteQuery } from '@tanstack/preact-query' | ||||||||||||
|
|
||||||||||||
| function Projects() { | ||||||||||||
| const fetchProjects = async ({ pageParam }) => { | ||||||||||||
| const res = await fetch('/api/projects?cursor=' + pageParam) | ||||||||||||
| return res.json() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const { | ||||||||||||
| data, | ||||||||||||
| error, | ||||||||||||
| fetchNextPage, | ||||||||||||
| hasNextPage, | ||||||||||||
| isFetching, | ||||||||||||
| isFetchingNextPage, | ||||||||||||
| status, | ||||||||||||
| } = useInfiniteQuery({ | ||||||||||||
| queryKey: ['projects'], | ||||||||||||
| queryFn: fetchProjects, | ||||||||||||
| initialPageParam: 0, | ||||||||||||
| getNextPageParam: (lastPage, pages) => lastPage.nextCursor, | ||||||||||||
| }) | ||||||||||||
|
|
||||||||||||
| return status === 'pending' ? ( | ||||||||||||
| <p>Loading...</p> | ||||||||||||
| ) : status === 'error' ? ( | ||||||||||||
| <p>Error: {error.message}</p> | ||||||||||||
| ) : ( | ||||||||||||
| <> | ||||||||||||
| {data.pages.map((group, i) => ( | ||||||||||||
| <div key={i}> | ||||||||||||
| {group.data.map((project) => ( | ||||||||||||
| <p key={project.id}>{project.name}</p> | ||||||||||||
| ))} | ||||||||||||
| <div/> | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix JSX closing tag syntax error. Line 45 has a self-closing tag 🔧 Proposed fix {group.data.map((project) => (
<p key={project.id}>{project.name}</p>
))}
- <div/>
+ </div>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
| ))} | ||||||||||||
| <div> | ||||||||||||
| <button | ||||||||||||
| onClick={() => fetchNextPage()} | ||||||||||||
| disabled={!hasNextPage || isFetching} | ||||||||||||
| > | ||||||||||||
| {isFetchingNextPage | ||||||||||||
| ? 'Loading more...' | ||||||||||||
| : hasNextPage | ||||||||||||
| ? 'Load More' | ||||||||||||
| : 'Nothing more to load'} | ||||||||||||
| </button> | ||||||||||||
| </div> | ||||||||||||
| <div>{isFetching && !isFetchingNextPage ? 'Fetching...' : null}</div> | ||||||||||||
| </> | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| [//]: # 'Example' | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: initial-query-data | ||
| title: Initial Query Data | ||
| ref: docs/framework/react/guides/initial-query-data | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: invalidations-from-mutations | ||
| title: Invalidations from Mutations | ||
| ref: docs/framework/react/guides/invalidations-from-mutations | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| id: mutations | ||
| title: Mutations | ||
| ref: docs/framework/react/guides/mutations | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- | ||
|
|
||
| [//]: # 'Info1' | ||
| [//]: # 'Info1' | ||
| [//]: # 'Example2' | ||
| [//]: # 'Example2' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: network-mode | ||
| title: Network Mode | ||
| ref: docs/framework/react/guides/network-mode | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: optimistic-updates | ||
| title: Optimistic Updates | ||
| ref: docs/framework/react/guides/optimistic-updates | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| --- | ||
| id: paginated-queries | ||
| title: Paginated / Lagged Queries | ||
| ref: docs/framework/react/guides/paginated-queries | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- | ||
|
|
||
| [//]: # 'Example2' | ||
|
|
||
| ```tsx | ||
| import { keepPreviousData, useQuery } from '@tanstack/react-query' | ||
| import { useState } from 'preact/hooks' | ||
|
|
||
| function Todos() { | ||
| const [page, setPage] = useState(0) | ||
|
|
||
| const fetchProjects = (page = 0) => | ||
| fetch('/api/projects?page=' + page).then((res) => res.json()) | ||
|
|
||
| const { isPending, isError, error, data, isFetching, isPlaceholderData } = | ||
| useQuery({ | ||
| queryKey: ['projects', page], | ||
| queryFn: () => fetchProjects(page), | ||
| placeholderData: keepPreviousData, | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| {isPending ? ( | ||
| <div>Loading...</div> | ||
| ) : isError ? ( | ||
| <div>Error: {error.message}</div> | ||
| ) : ( | ||
| <div> | ||
| {data.projects.map((project) => ( | ||
| <p key={project.id}>{project.name}</p> | ||
| ))} | ||
| </div> | ||
| )} | ||
| <span>Current Page: {page + 1}</span> | ||
| <button | ||
| onClick={() => setPage((old) => Math.max(old - 1, 0))} | ||
| disabled={page === 0} | ||
| > | ||
| Previous Page | ||
| </button> | ||
| <button | ||
| onClick={() => { | ||
| if (!isPlaceholderData && data.hasMore) { | ||
| setPage((old) => old + 1) | ||
| } | ||
| }} | ||
| // Disable the Next Page button until we know a next page is available | ||
| disabled={isPlaceholderData || !data?.hasMore} | ||
| > | ||
| Next Page | ||
| </button> | ||
| {isFetching ? <span> Loading...</span> : null} | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| [//]: # 'Example2' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| id: parallel-queries | ||
| title: Parallel Queries | ||
| ref: docs/framework/react/guides/parallel-queries | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- | ||
|
|
||
| [//]: # 'Info' | ||
|
|
||
| > When using Preact Query with compat's Suspense, this pattern of parallelism does not work, since the first query would throw a promise internally and would suspend the component before the other queries run. To get around this, you'll either need to use the `useSuspenseQueries` hook (which is suggested) or orchestrate your own parallelism with separate components for each `useSuspenseQuery` instance. | ||
|
|
||
| [//]: # 'Info' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: placeholder-query-data | ||
| title: Placeholder Query Data | ||
| ref: docs/framework/react/guides/placeholder-query-data | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --- | ||
| id: prefetching | ||
| title: Prefetching & Router Integration | ||
| ref: docs/framework/react/guides/prefetching | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- | ||
|
|
||
| [//]: # 'ExampleConditionally1' | ||
|
|
||
| ```tsx | ||
| // This lazy loads the GraphFeedItem component, meaning | ||
| // it wont start loading until something renders it | ||
| import { lazy } from 'preact/iso' | ||
|
|
||
| const GraphFeedItem = lazy(() => import('./GraphFeedItem')) | ||
|
|
||
| function Feed() { | ||
| const { data, isPending } = useQuery({ | ||
| queryKey: ['feed'], | ||
| queryFn: getFeed, | ||
| }) | ||
|
|
||
| if (isPending) { | ||
| return 'Loading feed...' | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {data.map((feedItem) => { | ||
| if (feedItem.type === 'GRAPH') { | ||
| return <GraphFeedItem key={feedItem.id} feedItem={feedItem} /> | ||
| } | ||
|
|
||
| return <StandardFeedItem key={feedItem.id} feedItem={feedItem} /> | ||
| })} | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| // GraphFeedItem.tsx | ||
| function GraphFeedItem({ feedItem }) { | ||
| const { data, isPending } = useQuery({ | ||
| queryKey: ['graph', feedItem.id], | ||
| queryFn: getGraphDataById, | ||
| }) | ||
|
|
||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| [//]: # 'ExampleConditionally1' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: queries | ||
| title: Queries | ||
| ref: docs/framework/react/guides/queries | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: query-cancellation | ||
| title: Query Cancellation | ||
| ref: docs/framework/react/guides/query-cancellation | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: query-functions | ||
| title: Query Functions | ||
| ref: docs/framework/react/guides/query-functions | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: query-invalidation | ||
| title: Query Invalidation | ||
| ref: docs/framework/react/guides/query-invalidation | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: query-keys | ||
| title: Query Keys | ||
| ref: docs/framework/react/guides/query-keys | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| id: query-options | ||
| title: Query Options | ||
| ref: docs/framework/react/guides/query-options | ||
| replace: { 'React': 'Preact', 'react-query': 'preact-query' } | ||
| --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing
useStateimport in Example 2.The code uses
useStatebut doesn't import it. This will cause a runtime error when users copy and run this example. Example 3 below demonstrates the correct import pattern.📝 Proposed fix to add the missing import
🤖 Prompt for AI Agents