Skip to content

Commit aa1ed8d

Browse files
authored
Merge pull request #223 from Nextjs-ja-translation/chore/update-advanced-features-dynamic-import
Chore: advanced/features/dynamic-import の差分追従
2 parents 85f3c50 + c051a2a commit aa1ed8d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

docs/advanced-features/dynamic-import.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: JavaScriptモジュールとReactコンポーネントを動的に
44

55
# 動的インポート
66

7-
<details>
7+
<details open>
88
<summary><b>例</b></summary>
99
<ul>
1010
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-dynamic-import">動的インポート</a></li>
@@ -13,8 +13,36 @@ description: JavaScriptモジュールとReactコンポーネントを動的に
1313

1414
Next.js は JavaScript の ES2020 [dynamic `import()`](https://github.com/tc39/proposal-dynamic-import) をサポートしています。JavaScript モジュール(React コンポーネントを含む)を動的にインポートして使うことが出来ます。SSR にも対応しています。
1515

16+
In the following example, we implement fuzzy search using `fuse.js` and only load the module dynamically in the browser after the user types in the search input:
17+
18+
```jsx
19+
import { useState } from 'react'
20+
const names = ['Tim', 'Joe', 'Bel', 'Max', 'Lee']
21+
export default function Page() {
22+
const [results, setResults] = useState()
23+
return (
24+
<div>
25+
<input
26+
type="text"
27+
placeholder="Search"
28+
onChange={async (e) => {
29+
const { value } = e.currentTarget
30+
// Dynamically load fuse.js
31+
const Fuse = (await import('fuse.js')).default
32+
const fuse = new Fuse(names)
33+
setResults(fuse.search(value))
34+
}}
35+
/>
36+
<pre>Results: {JSON.stringify(results, null, 2)}</pre>
37+
</div>
38+
)
39+
}
40+
```
41+
1642
動的インポートは、コードを扱いやすいチャンクに分割する、もう 1 つの方法と考えることができます。
1743

44+
React components can also be imported using dynamic imports, but in this case we use it in conjunction with `next/dynamic` to make sure it works like any other React Component. Check out the sections below for more details on how it works.
45+
1846
## 基本的な使用方法
1947

2048
次の例では、`../components/hello`モジュールがページで動的に読み込まれています:
@@ -39,6 +67,8 @@ export default Home;
3967

4068
`DynamicComponent``../components/hello` から返されるデフォルトのコンポーネントです。通常の React コンポーネントのように動き、通常どおりに props を渡すことができます。
4169

70+
> **Note**: In `import('path/to/component')`, the path must be explicitly written. It can't be a template string nor a variable. Furthermore the `import()` has to be inside the `dynamic()` call for Next.js to be able to match webpack bundles / module ids to the specific `dynamic()` call and preload them before rendering. `dynamic()` can't be used inside of React rendering as it needs to be marked in the top level of the module for preloading to work, similar to `React.lazy`.
71+
4272
## 名前付きエクスポート
4373

4474
動的コンポーネントがデフォルトのエクスポートでない場合は、名前付きエクスポートも使用できます。名前付きエクスポートの `Hello` を持つ `../components/hello.js` モジュールについて考えてみましょう:
@@ -116,3 +146,23 @@ function Home() {
116146

117147
export default Home;
118148
```
149+
150+
## With suspense
151+
152+
Option `suspense` allows you to lazy-load a component, similar to `React.lazy` and `<Suspense>` with React 18. Note that it only works on client-side or server-side with `fallback`. Full SSR support in concurrent mode is still a work-in-progress.
153+
154+
```jsx
155+
import dynamic from 'next/dynamic'
156+
const DynamicLazyComponent = dynamic(() => import('../components/hello4'), {
157+
suspense: true,
158+
})
159+
function Home() {
160+
return (
161+
<div>
162+
<Suspense fallback={`loading`}>
163+
<DynamicLazyComponent />
164+
</Suspense>
165+
</div>
166+
)
167+
}
168+
```

0 commit comments

Comments
 (0)