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

Function Posts

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected