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

Function Posts

examples/solid/default-query-function/src/index.tsx:63–109  ·  view source on GitHub ↗
(props: { setPostId: Setter<number> })

Source from the content-addressed store, hash-verified

61}
62
63function Posts(props: { setPostId: Setter<number> }) {
64 // All you have to do now is pass a key!
65 const state = useQuery<Array<Post>>(() => ({ queryKey: ['/posts'] }))
66
67 return (
68 <div>
69 <h1>Posts</h1>
70 <div>
71 <Switch>
72 <Match when={state.status === 'pending'}>Loading...</Match>
73 <Match when={state.status === 'error'}>
74 <span>Error: {(state.error as Error).message}</span>
75 </Match>
76 <Match when={state.data !== undefined}>
77 <>
78 <div>
79 <For each={state.data}>
80 {(post) => (
81 <p>
82 <a
83 onClick={() => props.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 'font-weight': 'bold',
91 color: 'green',
92 }
93 : {}
94 }
95 >
96 {post.title}
97 </a>
98 </p>
99 )}
100 </For>
101 </div>
102 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
103 </>
104 </Match>
105 </Switch>
106 </div>
107 </div>
108 )
109}
110
111function Post(props: { postId: number; setPostId: Setter<number> }) {
112 // You can even leave out the queryFn and just go straight into options

Callers

nothing calls this directly

Calls 3

useQueryFunction · 0.90
setPostIdMethod · 0.45
getQueryDataMethod · 0.45

Tested by

no test coverage detected