0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Next.js】名前付きエクスポートをダイナミックインポートする方法

Posted at

名前付きエスクポートをダイナミックインポートするには以下のように記述します。

components/hello.jsx
'use client'
 
export function Hello() {
  return <p>Hello!</p>
}

app/page.jsx
import dynamic from 'next/dynamic'
 
const Hello = dynamic(() =>
  import('../components/hello').then((module) => mod.Hello)
)

// ...

上記でError: Cannot access .then on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.というエラーが発生する場合には以下のように記述します。

import dynamic from 'next/dynamic'
 
const Hello = dynamic(async () => {
  const { Hello } = await import('../components/hello')
  return { default: Hello }
)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?