Skip to content

Commit

Permalink
docs: add an example for preload in effect (#345)
Browse files Browse the repository at this point in the history
* docs: add an example for preload in effects

* Update pages/docs/prefetching.en-US.md

Co-authored-by: Jiachi Liu <[email protected]>

* merge examples for inside React

* Update pages/docs/prefetching.en-US.md

Co-authored-by: Jiachi Liu <[email protected]>

* apply a suggestion

Co-authored-by: Jiachi Liu <[email protected]>
  • Loading branch information
koba04 and huozhi authored Aug 30, 2022
1 parent 5a01793 commit 602e161
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
13 changes: 11 additions & 2 deletions pages/docs/prefetching.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ It will prefetch the data when the HTML loads, even before JavaScript starts to

## Programmatically Prefetch

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.es-ES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Se precargarán los datos cuando se cargue el HTML, incluso antes de que el Java

## Programmatically Prefetch

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ JavaScriptのダウンロードが開始される前であっても、HTMLの読

## プログラムによるプリフェッチ

SWR は `preload` というデータをプログラマブルにプリフェッチして結果をキャッシュに保存する API を提供しています。`preload``key``fetcher` を引数として受け取ります。`preload` は React の外からも呼ぶことが可能です。
SWR は `preload` というデータをプログラマブルにプリフェッチして結果をキャッシュに保存する API を提供しています。`preload``key``fetcher` を引数として受け取ります。

`preload` は React の外からも呼ぶことが可能です。

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

またボタンをホバーしたタイミングでプリロードすることもできます
React のレンダリングツリー内においては, `preload` はイベントコールバックやエフェクトの中で利用可能です。

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// エフェクトの中でプリロードする
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* イベントコールバックの中でプリロードする */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ HTML `<head>` 안에 넣기만 하면 됩니다. 쉽고 빠르며 네이티브

## 프로그래밍 방식으로 프리패치

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.pt-BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Irá pré-obter os dados quando o HTML carregar, antes mesmo de iniciar a baixar

## Prefetching Programático

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

## Программная предварительная выборка

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down
13 changes: 11 additions & 2 deletions pages/docs/prefetching.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

## 手动预请求

SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments. You can call `preload` even outside of React.
SWR provides the `preload` API to prefetch the resources programmatically and store the results in the cache. `preload` accepts `key` and `fetcher` as the arguments.

You can call `preload` even outside of React.

```jsx
import { useState } from 'react'
Expand Down Expand Up @@ -43,15 +45,22 @@ export default function App() {
}
```

You can also preload it when hovering the button:
Within React rendering tree, `preload` is also avaiable to use in event handlers or effects.

```jsx
function App({ userId }) {
const [show, setShow] = useState(false)

// preload in effects
useEffect(() => {
preload('/api/user?id=' + userId, fetcher)
}, [useId])

return (
<div>
<button
onClick={() => setShow(true)}
{/* preload in event callbacks */}
onHover={() => preload('/api/user?id=' + userId, fetcher)}
>
Show User
Expand Down

0 comments on commit 602e161

Please sign in to comment.