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

Function Posts

examples/react/basic/src/index.tsx:36–82  ·  view source on GitHub ↗
({
  setPostId,
}: {
  setPostId: React.Dispatch<React.SetStateAction<number>>
})

Source from the content-addressed store, hash-verified

34}
35
36function Posts({
37 setPostId,
38}: {
39 setPostId: React.Dispatch<React.SetStateAction<number>>
40}) {
41 const queryClient = useQueryClient()
42 const { status, data, error, isFetching } = usePosts()
43
44 return (
45 <div>
46 <h1>Posts</h1>
47 <div>
48 {status === 'pending' ? (
49 'Loading...'
50 ) : status === 'error' ? (
51 <span>Error: {error.message}</span>
52 ) : (
53 <>
54 <div>
55 {data.map((post) => (
56 <p key={post.id}>
57 <a
58 onClick={() => setPostId(post.id)}
59 href="#"
60 style={
61 // We can access the query data here to show bold links for
62 // ones that are cached
63 queryClient.getQueryData(['post', post.id])
64 ? {
65 fontWeight: 'bold',
66 color: 'green',
67 }
68 : {}
69 }
70 >
71 {post.title}
72 </a>
73 </p>
74 ))}
75 </div>
76 <div>{isFetching ? 'Background Updating...' : ' '}</div>
77 </>
78 )}
79 </div>
80 </div>
81 )
82}
83
84const getPostById = async (id: number): Promise<Post> => {
85 const response = await fetch(

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
usePostsFunction · 0.70
getQueryDataMethod · 0.45

Tested by

no test coverage detected