Skip to content

Commit 7916648

Browse files
committed
docs: better basic example
1 parent 2283d26 commit 7916648

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

examples/basic/src/index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ function App() {
2828
);
2929
}
3030

31-
function Posts({ setPostId }) {
32-
const { status, data, error, isFetching } = useQuery("posts", async () => {
31+
function usePosts() {
32+
return useQuery("posts", async () => {
3333
const { data } = await axios.get(
3434
"https://jsonplaceholder.typicode.com/posts"
3535
);
3636
return data;
3737
});
38+
}
39+
40+
function Posts({ setPostId }) {
41+
const { status, data, error, isFetching } = usePosts;
3842

3943
return (
4044
<div>
@@ -76,23 +80,21 @@ function Posts({ setPostId }) {
7680
);
7781
}
7882

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.
8183
const getPostById = async (key, id) => {
8284
const { data } = await axios.get(
8385
`https://jsonplaceholder.typicode.com/posts/${id}`
8486
);
8587
return data;
8688
};
8789

90+
function usePost(postId) {
91+
return useQuery(["post", postId], getPostById, {
92+
enabled: postId,
93+
});
94+
}
95+
8896
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);
9698

9799
return (
98100
<div>

0 commit comments

Comments
 (0)