MCPcopy Create free account
hub / github.com/TanStack/query / Posts

Function Posts

examples/preact/default-query-function/src/index.tsx:61–107  ·  view source on GitHub ↗
({ setPostId }: { setPostId: (postId: number) => void })

Source from the content-addressed store, hash-verified

59}
60
61function Posts({ setPostId }: { setPostId: (postId: number) => void }) {
62 const queryClient = useQueryClient()
63
64 // All you have to do now is pass a key!
65 const { status, data, error, isFetching } = useQuery<Array<Post>>({
66 queryKey: ['/posts'],
67 })
68
69 return (
70 <div>
71 <h1>Posts</h1>
72 <div>
73 {status === 'pending' ? (
74 'Loading...'
75 ) : status === 'error' ? (
76 <span>Error: {error.message}</span>
77 ) : (
78 <>
79 <div>
80 {data.map((post) => (
81 <p key={post.id}>
82 <a
83 onClick={() => setPostId(post.id)}
84 href="#"
85 style={
86 // We can use the queryCache here to show bold links for
87 // ones that are cached
88 queryClient.getQueryData([`/posts/${post.id}`])
89 ? {
90 fontWeight: 'bold',
91 color: 'green',
92 }
93 : {}
94 }
95 >
96 {post.title}
97 </a>
98 </p>
99 ))}
100 </div>
101 <div>{isFetching ? 'Background Updating...' : ' '}</div>
102 </>
103 )}
104 </div>
105 </div>
106 )
107}
108
109function Post({
110 postId,

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected