@@ -6,29 +6,29 @@ id: data-fetching
6
6
keywords : [data fetching, react query, tanstack query, react-query, tanstack-query]
7
7
---
8
8
9
- Indeed, in the realm of React Native and React development, there are several valuable libraries that can significantly
9
+ Indeed, in the realm of React Native and React development, there are several valuable libraries that can significantly
10
10
simplify data management tasks. One such library is TanStack Query, which has gained popularity for its ability to streamline
11
- data fetching, caching, error handling, and more. It's an excellent choice for integrating into this boilerplate, as it can
11
+ data fetching, caching, error handling, and more. It's an excellent choice for integrating into this boilerplate, as it can
12
12
greatly enhance the efficiency and reliability of data management in your application.
13
13
14
14
## Fetching Data at Startup
15
15
16
- This boilerplate offers a convenient method for fetching data before presenting the application content to the user.
17
- This capability is made possible through the [ navigation structure] ( /docs/navigate#navigation-structure ) of the initial
16
+ This boilerplate offers a convenient method for fetching data before presenting the application content to the user.
17
+ This capability is made possible through the [ navigation structure] ( /docs/navigate#navigation-structure ) of the initial
18
18
setup and the inclusion of the ` Startup ` screen.
19
19
20
- The ` Startup ` screen takes on the role of being the very first screen shown to the user upon launching the application.
21
- It serves as the entry point where essential data can be fetched and prepared before the user interacts with the app's content.
20
+ The ` Startup ` screen takes on the role of being the very first screen shown to the user upon launching the application.
21
+ It serves as the entry point where essential data can be fetched and prepared before the user interacts with the app's content.
22
22
This ensures a smoother and more responsive user experience.
23
23
24
- ``` tsx title="src/screens/Startup/Startup.tsx"
24
+ ``` tsx title="src/screens/Startup/Startup.tsx"
25
25
// highlight-next-line
26
26
import { useQuery } from ' @tanstack/react-query' ;
27
27
28
28
const Startup = ({ navigation }: ApplicationScreenProps ) => {
29
29
const { layout, gutters, fonts } = useTheme ();
30
- const { t } = useTranslation ([ ' startup ' ] );
31
-
30
+ const { t } = useTranslation ();
31
+
32
32
// highlight-start
33
33
const { isSuccess, isFetching, isError } = useQuery ({
34
34
queryKey: [' startup' ],
@@ -38,7 +38,7 @@ const Startup = ({ navigation }: ApplicationScreenProps) => {
38
38
},
39
39
});
40
40
// highlight-end
41
-
41
+
42
42
useEffect (() => {
43
43
navigation .reset ({
44
44
index: 0 ,
@@ -54,36 +54,34 @@ const Startup = ({ navigation }: ApplicationScreenProps) => {
54
54
55
55
The ` useQuery ` hook is employed for data fetching. Now, let's explore how to formulate the request.
56
56
57
- Consider a scenario where we wish to retrieve application settings from an API before the user accesses the application's content.
57
+ Consider a scenario where we wish to retrieve application settings from an API before the user accesses the application's content.
58
58
To achieve this, we will create a service responsible for fetching this data.
59
59
60
- Within the ` services ` folder, create a file named ` startup/fetchSettings.ts ` , and include the following code:
61
-
62
- ``` ts title="src/services/startup/fetchSettings.ts"
60
+ ``` ts
63
61
import { instance } from ' @/services/instance' ;
64
62
65
63
export default async () => instance .get (` /settings ` );
66
64
```
67
65
68
- The ` instance ` is an Axios instance that comes pre-configured in the boilerplate.
66
+ The ` instance ` is an http client instance that comes pre-configured in the boilerplate.
69
67
70
68
Next, we will use the ` fetchSettings ` service within the ` Startup ` screen.
71
69
72
- ``` tsx title="src/screens/Startup/Startup.tsx"
70
+ ``` tsx title="src/screens/Startup/Startup.tsx"
73
71
import { useQuery } from ' @tanstack/react-query' ;
74
72
// highlight-next-line
75
- import fetchSettings from ' @/services/startup /fetchSettings' ;
73
+ import fetchSettings from ' @/folder /fetchSettings' ;
76
74
77
75
const Startup = ({ navigation }: ApplicationScreenProps ) => {
78
76
const { layout, gutters, fonts } = useTheme ();
79
77
const { t } = useTranslation ([' startup' ]);
80
-
78
+
81
79
const { isSuccess, isFetching, isError } = useQuery ({
82
80
queryKey: [' startup' ],
83
81
// highlight-next-line
84
82
queryFn: fetchSettings ,
85
83
});
86
-
84
+
87
85
useEffect (() => {
88
86
navigation .reset ({
89
87
index: 0 ,
@@ -99,5 +97,5 @@ const Startup = ({ navigation }: ApplicationScreenProps) => {
99
97
100
98
## Advanced usage
101
99
102
- Since we've utilized no additional or custom configuration, for further information,
100
+ Since we've utilized no additional or custom configuration, for further information,
103
101
you should refer to the official documentation of the [ library] ( https://react-query.tanstack.com/ ) .
0 commit comments