You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: pages/docs/getting-started.en-US.mdx
+2-2
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ import { Welcome } from 'components/diagrams/welcome'
93
93
94
94
Traditionally, we fetch data once using `useEffect` in the top level component, and pass it to child components via props (notice that we don't handle error state for now):
95
95
96
-
```jsxhighlight="7-11,17,18,27"
96
+
```jsx{7-11,17,18,27}
97
97
// page component
98
98
99
99
functionPage () {
@@ -141,7 +141,7 @@ components inside the page content can be dynamic, and the top level component m
141
141
142
142
SWR solves the problem perfectly. With the `useUser` hook we just created, the code can be refactored to:
Copy file name to clipboardexpand all lines: pages/docs/pagination.en-US.mdx
+7-7
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ import { Pagination } from 'components/diagrams/pagination'
23
23
...which is a typical pagination UI. Let's see how it can be easily implemented with
24
24
`useSWR`:
25
25
26
-
```jsxhighlight="5"
26
+
```jsx{5}
27
27
functionApp () {
28
28
const [pageIndex, setPageIndex] =useState(0);
29
29
@@ -42,7 +42,7 @@ function App () {
42
42
43
43
Furthermore, we can create an abstraction for this "page component":
44
44
45
-
```jsxhighlight="13"
45
+
```jsx{13}
46
46
functionPage ({ index }) {
47
47
const { data } =useSWR(`/api/data?page=${index}`, fetcher);
48
48
@@ -65,7 +65,7 @@ function App () {
65
65
Because of SWR's cache, we get the benefit to preload the next page. We render the next page inside
66
66
a hidden div, so SWR will trigger the data fetching of the next page. When the user navigates to the next page, the data is already there:
67
67
68
-
```jsxhighlight="6"
68
+
```jsx{6}
69
69
functionApp () {
70
70
const [pageIndex, setPageIndex] =useState(0);
71
71
@@ -95,7 +95,7 @@ import { Infinite } from 'components/diagrams/infinite'
95
95
To implement this, we need to make **dynamic number of requests** on this page. React Hooks have [a couple of rules](https://reactjs.org/docs/hooks-rules.html),
96
96
so we **CANNOT** do something like this:
97
97
98
-
```jsxhighlight="5,6,7,8,9"
98
+
```jsx{5,6,7,8,9}
99
99
functionApp () {
100
100
const [cnt, setCnt] =useState(1)
101
101
@@ -118,7 +118,7 @@ function App () {
118
118
119
119
Instead, we can use the `<Page />` abstraction that we created to achieve it:
120
120
121
-
```jsxhighlight="5,6,7"
121
+
```jsx{5,6,7}
122
122
functionApp () {
123
123
const [cnt, setCnt] =useState(1)
124
124
@@ -142,7 +142,7 @@ For example, we are still implementing the same "Load More" UI, but also need to
142
142
about how many items are there in total. We can't use the `<Page />` solution anymore because
143
143
the top level UI (`<App />`) needs the data inside each page:
144
144
145
-
```jsxhighlight="10"
145
+
```jsx{10}
146
146
functionApp () {
147
147
const [cnt, setCnt] =useState(1)
148
148
@@ -226,7 +226,7 @@ GET /users?page=0&limit=10
226
226
]
227
227
```
228
228
229
-
```jsx highlight="4,5,6,7,10"
229
+
```jsx {4,5,6,7,10}
230
230
// A function to get the SWR key of each page,
231
231
// its return value will be accepted by `fetcher`.
232
232
// If `null` is returned, the request of that page won't start.
Copy file name to clipboardexpand all lines: pages/docs/pagination.es-ES.mdx
+7-7
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ import { Pagination } from 'components/diagrams/pagination'
24
24
...que es un típico UI de paginación. Veamos cómo se puede implementar fácilmente con
25
25
`useSWR`:
26
26
27
-
```jsxhighlight="5"
27
+
```jsx{5}
28
28
functionApp () {
29
29
const [pageIndex, setPageIndex] =useState(0);
30
30
@@ -43,7 +43,7 @@ function App () {
43
43
44
44
Además, podemos crear una abstracción para este "page component":
45
45
46
-
```jsxhighlight="13"
46
+
```jsx{13}
47
47
functionPage ({ index }) {
48
48
const { data } =useSWR(`/api/data?page=${index}`, fetcher);
49
49
@@ -67,7 +67,7 @@ Gracias a la caché de SWR, tenemos la ventaja de precargar la siguiente página
67
67
La página siguiente se presenta dentro de un hidden div, por lo que SWR activará la obtención de datos
68
68
de la página siguiente. Cuando el usuario navega a la siguiente página, los datos ya están allí:
69
69
70
-
```jsxhighlight="6"
70
+
```jsx{6}
71
71
functionApp () {
72
72
const [pageIndex, setPageIndex] =useState(0);
73
73
@@ -97,7 +97,7 @@ import { Infinite } from 'components/diagrams/infinite'
97
97
Para implementar esto, necesitamos hacer **número de peticiones dinámicas** en esta página. Los React Hooks tienen [un par de reglas](https://reactjs.org/docs/hooks-rules.html),
98
98
por lo que **NO PODEMOS** hacer algo así:
99
99
100
-
```jsxhighlight="5,6,7,8,9"
100
+
```jsx{5,6,7,8,9}
101
101
functionApp () {
102
102
const [cnt, setCnt] =useState(1)
103
103
@@ -120,7 +120,7 @@ function App () {
120
120
121
121
En su lugar, podemos utilizar la abstracción `<Page />` que hemos creado para conseguirlo:
122
122
123
-
```jsxhighlight="5,6,7"
123
+
```jsx{5,6,7}
124
124
functionApp () {
125
125
const [cnt, setCnt] =useState(1)
126
126
@@ -144,7 +144,7 @@ Por ejemplo, seguimos implementando la misma UI "Load More", pero también neces
144
144
sobre cuántos item hay en total. No podemos utilizar la solución `<Page />` porque
145
145
la UI de nivel superior (`<App />`) necesita los datos dentro de cada página:
146
146
147
-
```jsxhighlight="10"
147
+
```jsx{10}
148
148
functionApp () {
149
149
const [cnt, setCnt] =useState(1)
150
150
@@ -228,7 +228,7 @@ GET /users?page=0&limit=10
228
228
]
229
229
```
230
230
231
-
```jsx highlight="4,5,6,7,10"
231
+
```jsx {4,5,6,7,10}
232
232
// Una función para obtener la key de SWR de cada página,
233
233
// su valor de retorno será aceptado por `fetcher`.
234
234
// Si se devuelve `null`, la petición de esa página no se iniciará.
0 commit comments