@@ -28,13 +28,17 @@ function App() {
28
28
) ;
29
29
}
30
30
31
- function Posts ( { setPostId } ) {
32
- const { status , data , error , isFetching } = useQuery ( "posts" , async ( ) => {
31
+ function usePosts ( ) {
32
+ return useQuery ( "posts" , async ( ) => {
33
33
const { data } = await axios . get (
34
34
"https://jsonplaceholder.typicode.com/posts"
35
35
) ;
36
36
return data ;
37
37
} ) ;
38
+ }
39
+
40
+ function Posts ( { setPostId } ) {
41
+ const { status, data, error, isFetching } = usePosts ;
38
42
39
43
return (
40
44
< div >
@@ -76,23 +80,21 @@ function Posts({ setPostId }) {
76
80
) ;
77
81
}
78
82
79
- // This function is not inline to show how query keys are passed to the query function
80
- // Normally, you can inline them if you want.
81
83
const getPostById = async ( key , id ) => {
82
84
const { data } = await axios . get (
83
85
`https://jsonplaceholder.typicode.com/posts/${ id } `
84
86
) ;
85
87
return data ;
86
88
} ;
87
89
90
+ function usePost ( postId ) {
91
+ return useQuery ( [ "post" , postId ] , getPostById , {
92
+ enabled : postId ,
93
+ } ) ;
94
+ }
95
+
88
96
function Post ( { postId, setPostId } ) {
89
- const { status, data, error, isFetching } = useQuery (
90
- [ "post" , postId ] ,
91
- getPostById ,
92
- {
93
- enabled : postId ,
94
- }
95
- ) ;
97
+ const { status, data, error, isFetching } = usePost ( postId ) ;
96
98
97
99
return (
98
100
< div >
0 commit comments