跳到主要內容
文件錯誤`next/dynamic` 已棄用一次載入多個模組

`next/dynamic` 已棄用一次載入多個模組

為何發生這個錯誤

`next/dynamic` 中已棄用一次載入多個模組的功能,以便更接近 React 的實作 (`React.lazy` 和 `Suspense`)。

更新依賴此行為的程式碼相對簡單!我們提供了一個修改前/後的範例,以幫助您遷移您的應用程式

可能的修正方式

遷移為對每個模組使用個別的 dynamic 呼叫。

修改前

example.js
import dynamic from 'next/dynamic'
 
const HelloBundle = dynamic({
  modules: () => {
    const components = {
      Hello1: () => import('../components/hello1').then((m) => m.default),
      Hello2: () => import('../components/hello2').then((m) => m.default),
    }
 
    return components
  },
  render: (props, { Hello1, Hello2 }) => (
    <div>
      <h1>{props.title}</h1>
      <Hello1 />
      <Hello2 />
    </div>
  ),
})
 
function DynamicBundle() {
  return <HelloBundle title="Dynamic Bundle" />
}
 
export default DynamicBundle

修改後

example.js
import dynamic from 'next/dynamic'
 
const Hello1 = dynamic(() => import('../components/hello1'))
const Hello2 = dynamic(() => import('../components/hello2'))
 
function HelloBundle({ title }) {
  return (
    <div>
      <h1>{title}</h1>
      <Hello1 />
      <Hello2 />
    </div>
  )
}
 
function DynamicBundle() {
  return <HelloBundle title="Dynamic Bundle" />
}
 
export default DynamicBundle