Skip to content
Source
Sign inSign up
TanStack/query

Keep data in useQuery result defined when initialData is defined #3557

Closedprateek3255(GitHub profile, opens in a new tab)wants to merge 5 commits intobeta fromrefs/pull/3557/headopened View on GitHub

Showing 5 changed files with 7 additions and 7 deletions (loading 2).

x
0 of 5 viewed, 5 left
1 addition and 0 deletionsexamples/basic-typescript/src/react-app-env.d.tsAdded
View file
@@ -0,0 +1 @@
+/// <reference types="react-scripts" />
5 additions and 6 deletionsexamples/react/basic-typescript/src/index.tsxModified
View file
@@ -28,11 +28,8 @@
function usePosts() {
return useQuery(
["posts"],
- async (): Promise<Array<Post>> => {
- const { data } = await axios.get(
- "https://jsonplaceholder.typicode.com/posts"
- );
- return data;
+ () => axios.get<Array<Post>>('https://jsonplaceholder.typicode.com/posts').then(({data}) => data), {
+ initialData: [{ id: 1, title: "Post 1", body: "Body 1" }],
);
@@ -44,6 +41,8 @@
}) {
const queryClient = useQueryClient();
const { status, data, error, isFetching } = usePosts();
+ // This shouldn't result in a TypeScript Error