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

Function Posts

examples/react/default-query-function/src/index.tsx:60–110  ·  view source on GitHub ↗
({
  setPostId,
}: {
  setPostId: React.Dispatch<React.SetStateAction<number>>
})

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected